I need to create a DataView from a DataTable, but with an unusual twist:
I need to decide (with code) on a row-by-row basis which rows in the DataTable are included in the DataView.
DataTable dt = LoadData();
DataView dv = new DataView(dt);
foreach (DataRow row in dt.Rows)
{
if (RowIsGood(row))
{
// This obviously doesn't work but I need equivalent logic:
DataRowView drv = new DataRowView();
drv.Row = row;
dv.Add(drv);
}
}
Some important things to note:
Is then even possible with the DataTable/DataView architecture?
Does LINQ allow me to customize a DataView like I need?
Since you are looking for a row by row solution and already have a function that takes a DataRow and returns a bool.
Simply add the refernce to System.Data.DataSetExtensions
And use the AsDataView
Method
DataView dv=DT.AsEnumerable().Where(RowIsGood).AsDataView();