I have the following code in a WinForms datagridview for handling right-mouse-clicks to select the underlying row:
private void dataGridViewTestSteps_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
var hitTestInfo = dataGridViewTestSteps.HitTest(e.X, e.Y);
dataGridViewTestSteps.ClearSelection();
dataGridViewTestSteps.Rows[hitTestInfo.RowIndex].Selected = true;
}
… now this works fine, but it does not place the small indicator in the correct row (see image below). So basically I was wondering what’s missing in the method above?

The row header cursor shows the current row, not the selected row – they are actually different, since you can have multiple selected rows but only one current row.
Try this code instead: