I've read a few questions asking how to achieve DataGridRow selection when there's a right-click on the grid. The answers show a few different ways to achieve it and, for most part, they've worked for me except for this weird bug.
The row appears to be selected but unless the row is left-clicked first, the first row is always the row selected when the action is chosen. i.e. When I click edit on row 3, row 1's data will be passed to the edit form (unless I left click row 3 before right-clicking it)
This is the right-click menu showing the apparent selection:
Notice the little indicator is still on the first row.
If I dismiss the context menu, the row looks selected but is not:
If I left-click the same row it is now selected
Here's the code for the right-click event:
Designer code:
MyDataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
Form code:
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = MyDataGrid.HitTest(e.X, e.Y);
MyDataGrid.CurrentCell = MyDataGrid.Rows[hti.RowIndex].Cells[hti.ColumnIndex];
}
}
What am I missing to actually select the row?




ContextMenuStripto a row. Also if you want the row get selected before showing the menu, you can handleCellContextMenuStripNeededevent. It seems this post is what you are looking for. - Reza Aghaei