Pergunta

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?

Foi útil?

Solução

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;
}

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top