Creating a Datatable out Array Rows in .Net
At certain point in time, we do come across a situation where we apply select filter on a Data table and get a array of rows. Now manipulating these rows is quite difficult for instance if we want these rows to act as a data source for a data grid then it becomes really trouble some kind of process. The function below FilterTable() gets data table and a filter string as an argument and returns a data table containing rows that satisfy the filter string.
Consider the case below where I have a datagrid and FilterTable() with its agrugument at the other end of assignment operator.(The code below is in C#)
dgSearch.DataSource = FilterTable(dsEvaluate.Tables[0], "IsValid like 'No' ") ;
private 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 <>
dr[dt.Columns[i].ColumnName] = oldDr[dt.Columns[i].ColumnName];
filteredDt.Rows.Add(dr);
}
return filteredDt;
}
Consider the case below where I have a datagrid and FilterTable() with its agrugument at the other end of assignment operator.(The code below is in C#)
dgSearch.DataSource = FilterTable(dsEvaluate.Tables[0], "IsValid like 'No' ") ;
private 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 <>
dr[dt.Columns[i].ColumnName] = oldDr[dt.Columns[i].ColumnName];
filteredDt.Rows.Add(dr);
}
return filteredDt;
}
Comments
Post a Comment