Click here to Skip to main content
11,805,316 members (65,482 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# DataGridView
I tested DataView.RowFilter and it works OK when the DataView is obtained from a single DataTable (created by DataTable constructor or filled by an adapter). However it doesn't work for a DataTable obtained from a DataSet (DataSet.Tables[index/TableName]). Could you please tell me how to make it work?

Or if possible, please give me another way to filter rows in a DataTable which is as an element of a DataSet???

Thank you!

Posted 22-Mar-13 5:26am
Edited 22-Mar-13 5:27am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can take an array of DataRow and use select method from the DataTable.
The code is explained below:

suppose you have a DataTable and let us say you have a column Age, on which you want apply filter,

dt is your DataTable.

DataRows[] drArrRow=dt.Select("Age =" + txtFilter.Text);

lets take another DataTable dtFilter and lets clone it from the dt DataTable

DataTable dtFilter = dt.Clone();

foreach (DataRow d in drArrRow)
dtFilter .ImportRow(d);
dataGridView1.DataSource = dtFilter;
//Re-Bind your DataGridView for the filtered result.

you are done with your filtered result.
supernorb at 23-Mar-13 13:05pm
Selecting of course works OK, however that's not what I want, selecting rows may lead to memory and performance problems, filtering is just a different thing. Thanks.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Check this
DataSet ds=getDataSet();
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "ID=12";
DataTable dt = dv.ToTable();
supernorb at 23-Mar-13 13:09pm
Of course, that's what I did, I also tried DataSet.DefaultViewManager.DataViewSettings["tableName"].RowFilter = "..." and of course this is likely the same to your solution, can't understand why it doesn't work, except the DataGridView's DataSource is a DataTable, a BindingSource, but in my case, it is a DataSet (and use DataMember to specify what table it's bound to). Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 3,265
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100