Selection issue in DataGridView

0
0

Hi,

I’m trying to move a row down in a DataGridView and keep it selected.
The  code I have works for moving a row up, but when I move it down, the row highlight appears in the correct place, but the triangle marker is still on the previous row and tracing through the code it looks like it still thinks the previous row is selected.

The code is as follows:

int index = dgvIPAddresses.CurrentCell.OwningRow.Index;
DataGridViewRowCollection rows = dgvIPAddresses.Rows;
// remove the next row and add it in front of the selected row.
DataGridViewRow nextRow = rows[index + 1];
rows.Remove(nextRow);
nextRow.Frozen = false;
rows.Insert(index, nextRow);
dgvIPAddresses.ClearSelection();
dgvIPAddresses.Rows[index + 1].Selected = true;
dgvIPAddresses.Refresh();

 

Any ideas ?
Thanks in advance,

Andi

  • You must to post comments
0
0

Thanks, that worked perfectly.

Andi

  • You must to post comments
0
0

Hi Andi,

what you see is the same in WinForms. You also need to change the CurrentRow. While this property is readonly you have to change it by setting the CurrentCell property.

Find some information here: https://social.msdn.microsoft.com/Forums/windows/en-US/47e9c3ef-a8de-48c9-8e0d-4f3fdd34517e/datagridview-select-row-programatically?forum=winformsdatacontrols

Adding the following code to your sample works as expected:

dataGridView1.CurrentCell = dataGridView1.Rows[index + 1][“Column0”];

(Column0 is just a sample name).

Best regards
Frank

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.