Converting DataRow Array to DataTable
Here's a little function I wrote the other day that I think might be helpful for some of you.
I was filtering a datatable using the DataTable.Select method. The Select method returns the filtered rows as an array of DataRow objects, but I wanted the result of the filter to be a Datable object containing the filtered rows, so I wrote a function that does this for me.
public DataTable FilterTable(DataTable dt, string filterString)
{
DataRow[] filteredRows=dt.Select(filterString);
DataTable filteredDt=dt.Clone();
DataRow dr;
foreach(DataRow oldDr in filteredRows)
{
dr=filteredDt.NewRow();
for(int i=0;i<dt.Columns.Count;i++)
dr[dt.Columns[i].ColumnName]=oldDr[dt.Columns[i].ColumnName];
filteredDt.Rows.Add(dr);
}
return filteredDt;
}
HTH
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using