Posted By

codernet on 01/18/12


Tagged

FilterDataGridViewDataViewRowLIKESearch


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

rcraven


Filter a DataGridView


 / Published in: C#
 

The above snippet can be used in any EventHandler such as TextChanged to Filter/Search a DataBound DataGridView according to a search term. When the TextBox from which the search parameter is taken will be empty the original DataGridViewRows will be filled. The RowFilter Propery can be given other SQL compatible operators such as >,<,<=

  1. if (txtSearchTextBox.Text != string.Empty)
  2. {
  3. DataView dvNewDataViewObject = new DataView();
  4. dvNewDataViewObject.Table = AnyDataset.AnyDataTableInsideIt;
  5. dvNewDataViewObject.RowFilter = "[AnyColumnOfDataTable] LIKE " + "'%" + txtSearchTextBox.Text + "%'";
  6. DataGridViewControl.DataSource = dvNewDataViewObject;
  7. DataGridViewControlOnForm.Refresh();
  8. }
  9. else
  10. {
  11. DataGridViewControlOnForm.DataSource = ActualorAnyBindingSource;
  12. ActualorAnyAdapter.Fill(AnyDataset.AnyDataTableInsideIt);
  13. DataGridViewControlOnForm.Refresh();
  14. }

Report this snippet  

You need to login to post a comment.