contextMenu は行に依存しているため、ContextMenu が表示される前に右クリックして dataGridView の行を選択する必要があります。
私はこれを試しました:
if (e.Button == MouseButtons.Right)
{
var hti = dataGrid.HitTest(e.X, e.Y);
dataGrid.ClearSelection();
dataGrid.Rows[hti.RowIndex].Selected = true;
}
また:
private void dataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGrid.Rows[e.RowIndex].Selected = true;
dataGrid.Focus();
}
}
これは機能しますが、dataGrid.Rows[CurrentRow.Index] を読み取ろうとすると、左クリックで選択された行のみが表示され、右クリックで選択された行は表示されません..