我需要在显示ContextMenu之前右键单击dataGridView中的一行,因为contextMenu是row-dependendt.
我试过这个:
if (e.Button == MouseButtons.Right)
{
var hti = dataGrid.HitTest(e.X, e.Y);
dataGrid.ClearSelection();
dataGrid.Rows[hti.RowIndex].Selected = true;
}
Run Code Online (Sandbox Code Playgroud)
要么:
private void dataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGrid.Rows[e.RowIndex].Selected = true;
dataGrid.Focus();
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但是当我尝试读取dataGrid.Rows [CurrentRow.Index]时,我只看到左键单击选中的行而不是右键单击选中的行.