LINQ Select a specific cell in the DataGridView depending on another cell in the row

I am a brand spanking a new one for LINQ and am trying to use it in my current hobby project. I have datagridviewwhere the first cell of each row is datagridviewcheckboxand the fourth cell is a row.

If checked, I need to add the 4th cell value to the list.

At first I tried:

var selectedID = from c in multiContactLookup.SelectedCells.Cast<DataGridViewCell>() 
                              select multiContactLookup.Rows[c.RowIndex].Cells[4].Value;

This did not work because the checked cells were not programmatically selected, therefore it is cnever a value.

Then I tried:

var sel2 = from r in multiContactLookup.Rows.Cast<DataGridViewRow>()
                       where r.Cells[0].Value is true select r.Cells[4].Value;

but for some reason my syntax is incorrect.

Using LINQ, how can I select the rows in which the first cell is checked, and then select the value of the first cell? Should I split it into two collections?

Thank!

+3
2

, :

IEnumerable<string> values = multiContactLookup.Rows.Cast<DataGridViewRow>()
    .Where(row => (bool)row.Cells[0].Value)
    .Select(row => (string)row.Cells[3].Value);
+8

, , , ...

DataGridView ( win-forms) LINQ. IEnumerable<T>. , Cast().

, . , DataTable. DataTable DataRow DataGridViewRow .

+2

Source: https://habr.com/ru/post/1755552/


All Articles