Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 22-Mar-13 5:27am
v2

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.
 
Share this answer
 
Comments
supernorb 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.
Check this
DataSet ds=getDataSet();
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "ID=12";
DataTable dt = dv.ToTable();
 
Share this answer
 
Comments
supernorb 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900