Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rows collection based on selected cells in DatagridView

I have a DatagridView control on a Windows form. It's selectionMode property is set to CellSelect.
I want to operate on DatagridViewRow based on selected cells. The DataGridView control is bound to a DataSource.

How to get the Row collection based on selected cells ?

like image 283
Vijay Balkawade Avatar asked Jan 19 '12 06:01

Vijay Balkawade


People also ask

How to get the selected value in DataGridView?

You can get the selected cells, rows, or columns from a DataGridView control by using the corresponding properties: SelectedCells, SelectedRows, and SelectedColumns.

How to select all rows in DataGridView c#?

CheckBox column selection All the rows in a datagrid can be selected by interacting with an intuitive check box in the column header.


1 Answers

The answer provided as Linq does not work with the syntax provided. Datagridview does not support ienumerable so you have to use:

        IEnumerable<DataGridViewRow> selectedRows = dgPOPLines.SelectedCells.Cast<DataGridViewCell>()
                                           .Select(cell => cell.OwningRow)
                                           .Distinct();
like image 158
Rupert Avatar answered Sep 23 '22 01:09

Rupert