我怎样才能筛选所有字段名C#的dataGridView? [英] How can i filter C# dataGridView across all field names?

查看:358
本文介绍了我怎样才能筛选所有字段名C#的dataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在这个影片它很容易添加一个文本框并将其驾驶DataGridView的过滤。问题是在这个视频中,似乎你有具体的哪一列的基础上进行筛选。

I see in this video its quite easy to add a textbox and have it drive the filtering of a datagridView. The issue is in this video it seems you have to specific which column to filter based on.

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

但是如果我想它来检查所有字段并显示该行,如果任一列中有

but what if I want it to check all of the fields and show the row if any column has my search string in it

推荐答案

您会通过你的行中的每一列要循环,并追加一个或比较

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

这篇关于我怎样才能筛选所有字段名C#的dataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆