DataGridView selection on right click
-
:confused: I have a DataGridView object and I added a ContextMenuStrip control to the DataGridView.ContextMenu. This works well, if I right-click the datagrid view I get the context menu I wanted. The problem is, I would like to, just before the context menu appears perform a left click to also select the cell or row under the mouse at that moment. Not sure what is the best way to do this. Thanks.
-
:confused: I have a DataGridView object and I added a ContextMenuStrip control to the DataGridView.ContextMenu. This works well, if I right-click the datagrid view I get the context menu I wanted. The problem is, I would like to, just before the context menu appears perform a left click to also select the cell or row under the mouse at that moment. Not sure what is the best way to do this. Thanks.
Hi, Johnny. This CellMouseDown event handler should work:
private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
}
} -
Hi, Johnny. This CellMouseDown event handler should work:
private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
}
}