3

在这个视频中看到它很容易添加一个文本框并让它驱动数据网格视图的过滤。问题出在此视频中,您似乎必须指定要根据哪一列进行过滤。

 RowFilter = "FirstName like "%' + searchText.Text + '%" 

但是如果我希望它检查所有字段并在任何列中有我的搜索字符串时显示该行怎么办

4

1 回答 1

5

You would want to loop through each column in your row and append an OR comparison

This is really stupid code, but hopefully gives you the gist of it. Something like:

StringBuilder filter = new StringBuilder();

foreach(var column in dataGridView.Columns)
{
   if(filter.ToString() == "")
   {
       filter.Append(column.Name + " like '" + searchText.Text + "'");
   }
   else 
   {
      filter.Append(" OR ");
      filter.Append(column.Name + " like '" + searchText.Text + "'");
   }
}

RowFilter = filter.ToString();
于 2013-06-06T01:32:44.457 回答