C# 如何将过滤器应用于具有多个“AND”条件的 DataView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19402801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply filter to DataView with Multiple "AND" conditions
提问by SHEKHAR SHETE
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.
我有一个DataTable
包含一些行。复制到DataView
. 现在我有List<string>
. 其中包含从 中选择的项目GridView
。现在我想DataView
使用 AND 作为过滤器来过滤它。
When I apply just one it works, but applying multiple AND
doesn't work.
当我只应用一个时,它可以工作,但应用多个AND
不起作用。
In .cs :
在 .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?
任何的想法?
采纳答案by Somnath
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;
}
回答by SHEKHAR SHETE
As described in MSDNThe value has to be within quotation marks. So the following should work:
如MSDN 中所述,该值必须在引号内。所以以下应该工作:
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.
无论如何,当您的列表包含要显示的 ID 时,运算符应该是 OR 而不是 AND,因为表中不应该有同时具有两个 ID 的行。