Wednesday, April 18, 2012

DataSource a Linq(ed) DataTable example

if u ever tried to query with Linq a datatable and then assign it to a DataGridView (and other stuff i guess) then u might have noticed that the grid shows the properties of the DataRow(s).
And if you tried to select the ItemArray, again a mess.
Sooo... its so simple i laugh at myself but i must:

var query =
     from data in tblAllData.AsEnumerable()
     where data.Field<DateTime>("Start").Month == (int)cmbMonths.SelectedValue
     select data;
if (query.Count() > 0)                
    dgvMain.DataSource = query.CopyToDataTable();

TADA!

No comments:

Post a Comment