SelectCommand.Parameters.AddWithValue("@ID", ProductID);
da.TableMappings.Add("Table", "Products");
da.Fill(ds);
DataTable dt = ds.Tables["Products"];
IEnumerable
prod =
from p in dt.AsEnumerable()
select p;
if (prod.Count() > 0)
{
DataTable dat = prod.CopyToDataTable(); i
grdProducts.DataSource = dat;
}
else
{
grdProducts.DataSource = null;
}
}
321
Appendix A: Case Study
When the user selects a new product type, the corresponding products are retrieved from the database
via a DataAdapter and used to fill a DataTable. A LINQ query is then executed to query the contents of
the DataTable, at which point the CopyToDataTable method is used to return a DataTable with copies
of the DataRow objects using an input IEnumerable object.
The new DataTable is used as the data source for the products grid.
The SelectProduct form needs to be opened from the main form. To make that work, open the main
form and add the following highlighted code to Products button??™s Click() event.
private void cmdProducts_Click(object sender, EventArgs e)
{
SelectProduct selectProd = new SelectProduct();
selectProd.
Pages:
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500