anon
anon

Reputation:

Programmatically multiselect rows in WinForms DatagridView properly

Is it possible and if so, how, to programmatically select non-consecutive rows in a standard WinForms datagrid?

foreach (var selectableRowIndex in selectableRowIndices)
{
    dataGridView.Rows[selectableRowIndex ].Selected = true;
}

... does work, but after setting the dataGridView's .CurrentCell Property via

dataGridView.CurrentCell = dataGridView.Rows[underlyingRowIndex].Cells[1];

all other rows get deselected.

What's the proper way to set the current row/cell and keep other rows selected?

Upvotes: 0

Views: 1424

Answers (2)

Jc Gomez Tagle
Jc Gomez Tagle

Reputation: 11

I had same problem and my solution was to have a List<Int32> with the ids of the selected rows and to add or remove the ids as needed.

Upvotes: 1

DarkSquirrel42
DarkSquirrel42

Reputation: 10287

a quick and dirty solution would be:

-get the IDs of the currently selected rows
-change the current cell
-re-select the rows

Upvotes: 0

Related Questions