wpf 循环中的 DataView.Rowfilter 多个过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13063726/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:56:33  来源:igfitidea点击:

DataView.Rowfilter multiple filter in Loop

c#wpfdataviewrowfilter

提问by Kishor

DataView DV = DataTable.AsDataView();
for(int i=0; i<ConditonQueue.Count; i++)
{
     DV.RowFilter = ConditonQueue.ElementAt(i);
}

Here after the loop DVis filtered only for last element in ConditionQueue. How can I get DVwith all filter condition on queue applied? ConditonQueue.ElementAt(i)returns a string, which is a valid expression for RowFilter.

在此循环之后,DV仅针对 中的最后一个元素进行过滤ConditionQueue。如何DV应用队列上的所有过滤条件? ConditonQueue.ElementAt(i)返回一个字符串,它是 的有效表达式RowFilter

Combining all ConditonQueue.ElementAt(i)will not help in my scenario. I want to apply RowFiltereach time.

ConditonQueue.ElementAt(i)在我的情况下,将所有内容结合起来无济于事。RowFilter每次都想申请。

As I need to do some calculation after each RowFilter. ANDing all conditions will not help.

因为我需要在每个RowFilter. AND所有条件都无济于事。

Is there any other way to recreate the DVwhen RowFilteris applied in loop each time?

是否有任何其他的方式来重新创建DV时,RowFilter在每次循环应用?

回答by codingbiz

Lots of assumption as your question is not clear. Try this

很多假设,因为你的问题不清楚。尝试这个

DataView DV = DataTable.AsDataView();

string[] filter = new string[ConditonQueue.Count];
for(int i=0; i<ConditonQueue.Count; i++)
{
     filter[i] =  ConditonQueue.ElementAt(i).ToString();
}

DV.RowFilter = String.Join(" AND ", filter); // filter1 AND filter2 AND ... AND filterN

I don't like your approach, am sorry to say but I think you have to update the DV so that the new filter is applied to already filtered view

我不喜欢您的方法,很抱歉,但我认为您必须更新 DV,以便将新过滤器应用于已过滤的视图

for(int i=0; i<ConditonQueue.Count; i++)
{
     DV.RowFilter = ConditonQueue.ElementAt(i);
     DV = DV.ToTable().AsDataView();
}

回答by Daniel

The RowFilter ist automatically applied to all rows in the data view. The correct usage would be to combine all conditions of the queue into an appropriate string expression and pass that expression to the RowFilter property.

RowFilter 会自动应用于数据视图中的所有行。正确的用法是将队列的所有条件组合成适当的字符串表达式,并将该表达式传递给 RowFilter 属性。

Also have a look at http://www.csharp-examples.net/dataview-rowfilter/for examples of the RowFilter syntax.

还可以查看http://www.csharp-examples.net/dataview-rowfilter/以获取 RowFilter 语法的示例。