Filtering DataView with multiple columns

25,879

You should apply your filter altogether, not one by one :

newdv.RowFilter = "Column1 = " + value1 + " AND Column2 = " + value2;

So you can change your code as :

string[] items = str.Split(',');
string filter = string.Empty;
for (int i = 0; i < items.Length; i++)
{
    filter += items[i] + " = " + dropdown.SelectedValue;
    if (i != items.Length - 1)
    {
         filter += " AND ";
    }
}
newdv.RowFilter = filter;
Share:
25,879
Admin
Author by

Admin

Updated on December 04, 2020

Comments

  • Admin
    Admin over 3 years

    In my application I am using a dataview for having the filters to be applied where the filter options are passed dynamically.if there are 2 filter parameters then the dataview should be filtered for parameter1 and then by parameter two. I am using a method which is called in a for loop where I am setting the count to the total no.of parameters selected using a listbox but the filtering is done only for the last parameter. Here is my code:

    string str = "";
    for (int i = 0; i < listbox.Items.Count; i++)
    {
        if (listbox.Items[i].Selected)
        {
            if (str != string.Empty)
            {
                str = str + "," + listbox.Items[i].Text;
    
            }
            else
            {
                str = str + listbox.Items[i].Text;
            }
        }
    }
    
    string[] items = str.Split(',');
    for (int i = 0; i < items.Length; i++)
    {
        ApplyFilter(items[i],dv);
    }
    
    private DataView ApplyFilter(string str,DataView newdv)
    {
        newdv.RowFilter = "[" + str + "]=" + ddl.SelectedItem.ToString();
    
        return newdv;
    }
    

    Please provide a suitable solution .

    Thanks in advance...

  • Admin
    Admin almost 15 years
    but iam not sure of how many filter parameters will be passed.how to add the columns in the same rowfilter