如何基于多个输入过滤DataView
我知道如何根据用户在单个文本框中的输入过滤数据:
FilterDataView.RowFilter = txtFilter.Text;
但是,您将如何根据多个字段的多个用户输入过滤数据。 基本上filter将充当“搜索”function。
定义RowFilter时可以使用light t-sql之类的东西。
一个想法是:
FilterDataView.RowFilter = "name like '%habjan%' and city like '%new york%'"
在这里,您可以找到关于RowFilter语法的好文章: DataView RowFilter语法
根据您的需要,您必须根据输入的字段构建行filter。
StringBuilder sb = new StringBuilder(); if (tb1.Text.Length > 0) { sb.Append("name like '%" + tb1.Text + "%'"); } if (tb2.Text.Length > 0) { if(sb.Length > 0) { sb.Append(" and "); } sb.Append("city like '%" + tb2.Text + "%'"); } //.... and so on... FilterDataView.RowFilter = sb.ToString();
搜索MSDN(LINQ to DataSet) http://msdn.microsoft.com/en-us/library/bb669073.aspx
好的,Linq to SQL将成为你的朋友。
您需要学习语法,并且有一些不同的表单,但您可以通过以下方式重新创建查询:
var x = from T in db.Table where [usual where stuff goes here];
然后,您可以测试每个文本框并执行以下操作:
if (TextBox1.Text != null) x.Where(w => w.field1 == val1);
好的是你可以一次分层一个Where子句。 然后将对象绑定到var。
[objectname].Datasource = x; [objectname].Databind();
这应该可以解决你的问题。