Filter a DataGridView


/ Published in: C#
Save to your folder(s)

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 >,


Copy this code and paste it in your HTML
  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


Comments


You need to login to view comments.