DataView RowFillter like operator

别说谁变了你拦得住时间么 提交于 2019-12-08 06:01:55

问题


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.

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 + "'";

回答1:


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();


来源:https://stackoverflow.com/questions/17313631/dataview-rowfillter-like-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!