筛选具有多列的DataView [英] Filtering DataView with multiple columns
问题描述
在我的应用程序中,我正在使用一个dataview来应用要应用的过滤器,其中过滤器选项是动态传递的。如果有2个过滤器参数,则应为data1过滤dataview,然后通过参数2过滤。我正在使用一种在for循环中调用的方法,其中我将计数设置为使用列表框选择的参数总数,但仅对最后一个参数进行过滤。
这是我的代码:
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 .
预先感谢...
推荐答案
您应该完全应用过滤器,而不是一个一个地应用:
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;
这篇关于筛选具有多列的DataView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!