I’ve this linq query and don’t know why it is not working. itemDetails is my Datatable.I am a beginner.
What I want is to get the itemID for an item whenever it is selected in the datagridview combobox field.
1) One problem is with the linq query I’ve written below.
from r in itemDetails
where r.ItemName = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()
select r.ItemID;
It is giving the following error
Could not find an implementation of the query pattern for source type
‘System.Data.DataTable’. ‘Where’ not found.
2) The other problem is that how can I work on datagridview combobox selectedIndexchanged event
The problem you’re having is that a DataTable is not an IEnumerable class, meaning you will not be able to use Linq methods on it. That’s what the compiler’s complaining about; the Linq query you’ve written is compiled into a series of method calls, and the compiler expects to find a Where() method that it can call on a DataTable to perform the where clause’s filtering, and doesn’t find one.
However, its Rows property is IEnumerable (but not generic, so you’ll need to use the OfType() method to create an IEnumerable of DataRows):
Now, on top of that, DataRow does not have properties ItemName and ItemID. You don’t get values from a DataRow that way. The compiler isn’t complaining about that in your code (yet) because it doesn’t even know what the type of
ris, so it can’t determine whether objects of that type have those members. Once you specify that r is a DataRow, the compiler will pick up the additional errors.To fix that, you’ll need to use the indexer available on a DataRow. You can either refer to the columns by index (zero-based from left to right based on the select list of the query that hydrated the DataTable), or by column name (again based on the names of the columns or the aliases you give them in the select list):
The items produced by the indexer are of type Object; you will need to cast or convert them to the proper type (I’m assuming, for instance, that ItemName is a String and that ItemId is a 32-bit integer).
As for your second question, DataGridViews don’t expose most of the events of their contained controls. Instead, those controls are hooked into, and cause the DGV to fire its own events which you can handle. Take a look at the CellValueChanged event and/or the CellDirtyStatusChanged event. The first one is raised when the user changes a cell’s value and then that cell loses focus. CellDirtyStatusChanged is raised as soon as the first change is made to the cell’s value, before a commit actually occurs.