- dataGridView1.DataSource = prodotti.ToArray();
This line of code will display in the DataGrid control all the content of the Collection Products. So far, we have displayed all list data products in the DataGrid control, now comes the less simple, how to display the products on the basis of a selection. For example, view the data quantity and the price of all the products that year equal to Id 1. Again we should create a class that will allow you to view data of products, for example:
- public class Dettagli
- {
- public int Quantita{ get; set; }
- public double Prezzo{ get; set; }
- }
Finally, a simple LINQ query where, based on the value of the field, It'll display the quantity and the price.
- private void button1_Click(object sender, EventArgs e)
- {
- var result = from a in prodotti
- where a.Id == "1"
- select new Dettagli{ Quantita = a.Quantita, Prezzo = a.Prezzo };
- dataGridView2.DataSource = result.ToList();
- }
After the execution of the search queries we thus get all orders executed by Id order equal to 1. LinqToObjects is not limited to keyword or Select Where used in this example, but it has many keywords, for more information on MSDN Library examples are explained in detail.