Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply filter to DataView with Multiple "AND" conditions

I have a DataTable containing the some rows. Which is copied to DataView. Now I have IDs in form of List<string>. which contains the selected items from GridView. Now I want to filter this DataView using AND as filter.

When I apply just one it works, but applying multiple AND doesn't work.

In .cs :

List<string> selectedAddress = new List<string>();
protected DataView GetSelectedItems()
{
    DataView dv = new DataView(dtresult);
    int count = selectedAddress.Count();
    if (count > 0)
    {
        string query = "ID=";

        for (int j = 0; j < selectedAddress.Count; j++)
        {
            string val = selectedAddress[j].ToString();
            if (j == 0)
            {
                query += val + " and ";
            }
            else
            {
                query += "ID=" + val + "";
            }
        }
        dv.RowFilter = query;
    }
    return dv;
}

Any idea?

like image 350
SHEKHAR SHETE Avatar asked Sep 11 '25 18:09

SHEKHAR SHETE


2 Answers

Try this:

List<string> selectedAddress = new List<string>();
protected DataView GetSelectedItems()
{
    DataView dvResult = new DataView(dtresult);
    string query = "";
    int count = selectedAddress.Count();

    for (int j = 0; j < selectedAddress.Count; j++)
    {
        string val = selectedAddress[j].ToString() + ",";
        query += val;
    }

    query = query.Remove(query.Length - 1);
    dvResult.RowFilter = "ID IN(" + query + ")";
    return dvResult;
}
like image 138
Somnath Avatar answered Sep 14 '25 09:09

Somnath


As described in MSDN The value has to be within quotation marks. So the following should work:

dv.RowFilter = "ID = '23' or ID = '46'";

Anyway when your list contains the IDs to show then the operator should be OR and not AND as there should be no row in the table having two IDs at the same time.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!