DataGridView Filtering

I am trying to filter out a DataGridView, a DataSource is a DataSet.

So, I use the following line to filter the grid:

DataTable dt = (dataGridViewMain.DataSource as DataSet).Tables[0];
dt.DefaultView.RowFilter = "CustomerName = 'My Customer'";

However, the grid is not filtered, and all rows are still displayed. What am I missing?

+3
source share
3 answers

Then you need to rebuild the grid. I would suggest you call the function data, get the data set, and then apply the row filter:

var view=GetDataSet().Tables[0].DefaultView
view.RowFilter = "CustomerName = 'My Customer'";
dataGridViewMain.DataSource=view;
dataGridViewMain.DataBind();

Otherwise, you may have to do this:

var view=(dataGridViewMain.DataSource as DataSet).Tables[0].DefaultView
view.RowFilter = "CustomerName = 'My Customer'";
dataGridViewMain.DataSource=view;
dataGridViewMain.DataBind();
+2
source

When you bind your controls, do you bind them to DefaultView or DataTable? Binding to a DataTable will never show the RowFilter that you have against DefaultView.

+1
source

rowstate DataViewRowState.ModifiedCurrent. datatable datagrid.

(dataGridViewMain.DataSource as DataSet).Tables[0].AcceptChanges();
dataGridViewMain.DataBind();
0
source

All Articles