开发者

Programmatically multiselect rows in WinForms DatagridView properly

开发者 https://www.devze.com 2023-03-23 02:56 出处:网络
Is it possible and if so, how, to programmatically select non-consecutive rows in a standard WinForms datagrid?

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

foreach (var selectableRowIndex in select开发者_StackOverflowableRowIndices)
{
    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?


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.


a quick and dirty solution would be:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消