Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGridView WinForm
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!
 
VipHaLong.
Posted 22-Mar-13 6:26am
supernorb2.6K
Edited 22-Mar-13 6:27am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
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.
  Permalink  
Comments
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
good
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();
  Permalink  
Comments
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 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2014
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