Search code examples
c#filterdatatabledataviewdata-filtering

How to apply two filters in DataView


I am trying to apply two filters in a DataView such that first it should filter items of one column, and then those values should further be filtered by a second column. below is my code, but this code is just filtering both separately.

DataView dv = new DataView(dt);
                dv.RowFilter = string.Format("[Animal Id] Like '%{0}%'", comboBox6.Text.Trim());
                dv.RowFilter = string.Format("Step Like '%{0}%'", comboBox5.Text.Trim());

Solution

  • this code is just filtering both separately.

    That's because the second filter overwrites the first. You appear to be saying you want an AND situation

    dv.RowFilter = string.Format("[Animal Id] Like '%{0}%' AND [Step] Like '%{1}%'", comboBox6.Text.Trim(), comboBox5.Text.Trim());
    

    Please rename your controls to something better than textBox27, label35. Renaming controls takes seconds costs nothing and brings immense value that anyone who reads the code has a chance at understanding it, especially if theyre on e.g. the internet and cannot swap back to the form constantly to see whether the first name is in textBox45, or textBox22