user1131661
user1131661

Reputation: 229

Filter a DataGridView

I'm trying to filter a DataGridView, the DataSource is a DataSet.

So I'm using the following line to filter the grid:

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

However the grid doesn't get filtered and all the rows are still displayed. What am I missing?

Upvotes: 3

Views: 4255

Answers (3)

Freak Fly
Freak Fly

Reputation: 11

When you are binding your controls, are you binding them to the DefaultView or to the DataTable? Binding to the DataTable will never show the RowFilter you have against the DefaultView.

Upvotes: 1

Krishnanunni Jeevan
Krishnanunni Jeevan

Reputation: 1759

Try setting the rowstate filter to DataViewRowState.ModifiedCurrent.Also accept the changes for datatable and rebind the datagrid.

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

Upvotes: 0

Arion
Arion

Reputation: 31239

You need to rebind the grid then. I would suggest you calling the function data get the dataset and then apply the rowfilter:

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

Otherwise you might have to do this:

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

Upvotes: 2

Related Questions