Place the DataGridView row indicator for the manually selected row

I have the following code in datagridview WinForms for handling right clicks to select a base row:

private void dataGridViewTestSteps_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Right) return; var hitTestInfo = dataGridViewTestSteps.HitTest(eX, eY); dataGridViewTestSteps.ClearSelection(); dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Selected = true; } 

... now this works fine, but does not put the small indicator on the correct line (see image below). So basically I was wondering what is missing in the method above?

falsely placed row indicator

+6
source share
1 answer

The row header cursor shows the current row, not the selected row β€” they are actually different, since you can have several rows selected, but only one current row.

Try using this code:

 private void dataGridViewTestSteps_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Right) return; var hitTestInfo = dataGridViewTestSteps.HitTest(eX, eY); //dataGridViewTestSteps.ClearSelection(); //dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Selected = true; dataGridViewTestSteps.CurrentCell = dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Cells[0]; } 
+5
source

Source: https://habr.com/ru/post/893505/


All Articles