C# DataView RowFillter 类似操作符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17313631/
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
DataView RowFillter like operator
提问by Dillibabu
i am facing small issue in dataview rowfilter. How to handle textbox empty value in dataview rowfilter.i am using OR operator in this filter. Please help Guide me in this issue.so far i using below code.
我在 dataview rowfilter 中遇到了小问题。如何处理数据视图 rowfilter 中的文本框空值。我在此过滤器中使用 OR 运算符。请帮助指导我解决这个问题。到目前为止,我使用以下代码。
Column1 = string.IsNullOrEmpty(txtColumn1.Text) ? "" : "%" + txtColumn1.Text + "%";
Column2 = string.IsNullOrEmpty(txtColumn2.Text) ? "" : "%" + txtColumn2.Text + "%";
Column3 = string.IsNullOrEmpty(txtColumn3.Text) ? "" : "%" + txtColumn3.Text + "%";
dataView.RowFilter = @"Column1 like '" + Column1 + "'" + "OR Column2 like '" + Column2 + "'" + "OR Column3 like '" + Column3 + "'";
采纳答案by Vimal CK
The below code snippet will help you to control filtering with empty values. Plase try and mark the answer if it useful
下面的代码片段将帮助您使用空值控制过滤。如果有用,请尝试并标记答案
StringBuilder filter = new StringBuilder();
if (!(string.IsNullOrEmpty(textBox1.Text)))
filter.Append("Column1 Like '%" + textBox1.Text + "%'");
if (!(string.IsNullOrEmpty(textBox2.Text)))
{
if (filter.Length > 0) filter.Append(" OR ");
filter.Append("Column2 Like '%" + textBox2.Text + "%'");
}
if (!(string.IsNullOrEmpty(textBox3.Text)))
{
if (filter.Length > 0) filter.Append(" OR ");
filter.Append("Column3 Like '%" + textBox3.Text + "%'");
}
DataView dv = dt.DefaultView;
dv.RowFilter = filter.ToString();

