Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i filter C# dataGridView across all field names?

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

like image 224
leora Avatar asked Sep 19 '25 02:09

leora


1 Answers

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();
like image 107
Tom Studee Avatar answered Sep 20 '25 18:09

Tom Studee