0
votes

I have created a DataGridView in a forms based application. I have preselected some some rows based on some conditions using the following code: DataGridView1.Rows[i].Selected = true;

now I have a button which allows the user to move the focus(record pointer) to the next selected row.

When the user clicks the button I do the following DataGridView1.CurrentCell = DataGridView1[0,row_number];

but when this happens the previously selected row loses the highlight.

I want to maintain the highlight (selection) of the previous row also but the record pointer should be moved to the next selected row. How can I acheive this.

1

1 Answers

1
votes

When you make DataGridView1.CurrentCell equal to a single cell (row), the multiselection is converted into single selection and thus all the other rows are deselected (Selected property set to False).

There are various options to avoid that:

  • The most logical option is not relying on DataGridView1.CurrentCell at all. You can create your own variable to store the current cell being edited (you might even store it in the DataGridView itself, via its Tag property).
  • Another option would be affecting the style (background color) of the rows selected sofar to transmit to the user the impression that they are still selected. Although this second option is not too recommendable, as far as all the "associated features" (e.g., Selected property) would continue behaving as if these rows wouldn't be selected.