Home > Uncategorized > Filtering a DataSet

Filtering a DataSet

I know that .NET offers a number of ways to show different filtered views of a DataSet, however, if you want to actually change the DataSet, you run into nasty little errors such as "This row already belongs to another table" or "These columns dont currently have unique values" etc.
 
So here’s a method I made for filtering datasets…

public static DataSet FilterDataSet(DataSet ds,string TableName,string column, string filter)

{

DataSet dsCopy = ds.Copy();

ArrayList alRowsToRemove = new ArrayList();

foreach(DataRow dr in dsCopy.Tables[TableName].Rows)

{

if (dr[column].ToString()!=filter)

{

alRowsToRemove.Add(dr);

}

}

foreach(DataRow dr in alRowsToRemove)

{

dsCopy.Tables[TableName].Rows.Remove(dr);

}

return dsCopy;

}

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: