Take the 2-minute tour ×

I have just finished some code that is successfully filtering deadlines that fall between 2 datetimepickers - startSchedule and endSchedule.

At the moment the if statement is initiated when I want it to be but I'm not sure which code to put in the if statement to display the filtered rows in the datagridview.

The datagridview has the following columns - module, description, deadline, priority.

How do I display the appropriate rows in the if statement that meet the conditions.

Let me know if you need any more information, thankyou.

Here is the code I have so far:

 private void scheduleButton_Click(object sender, EventArgs e)

        DateTime startSchedule = startDate.Value.Date;
        DateTime endSchedule = endDate.Value.Date;
            foreach (DataGridViewRow dr in TaskTable2.Rows)
                    string deadline = dr.Cells["Deadline"].Value.ToString();
                    DateTime deadlineRow = Convert.ToDateTime(deadline);   
                    if (startSchedule <= deadlineRow && deadlineRow <= endSchedule)
                        MessageBox.Show("Display Row"); // display filtered rows here.

share|improve this question
Anyone have an idea of what code to put in the if statement code section? –  Rob Mar 8 '12 at 20:12
I assume the rows that meet the requirements of the if statement are the rows you want to be visible, or do I have that backwards @user1163762? –  ImGreg Mar 8 '12 at 20:29
add comment

2 Answers

up vote 1 down vote accepted

Depends on how you have your DataGridView setup. Ideally you would probably do this from the SQL side of things, but you can do it this way if you prefer.

Perhaps just hide the row if your ifstatement is NOT met:

dr.Visible = false;

Let me know if I am misunderstanding what you want.

share|improve this answer
add comment

Counter-question: What are you using as the .Datasource of the DataGridView?

It's a fine line in concept, but you do not actually filter the rows of the DataGridView. Rather, you filter the data source that the DGV is displaying. For example, if you have a DataTable as the data source, you can use the DataTable.Select method to filter the data source. As my apps usually have business objects, I use TimVW's FilterList as a generic list of objects, which I set as the data source to my DGVs. The FilterList has a .Filter() method to filter the data source. As soon as the object in the DGV's .Datasource property is filtered, the DGV will update the rows displayed on-screen.

share|improve this answer
add comment

Your Answer


