contextMenuがrow-dependendtなので、ContextMenuが表示される前に右クリックしてdataGridViewの行を選択する必要があります。dataGridViewの行を右クリックして選択します
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]私は左で選択した行のみを参照してください。
if (e.Button == MouseButtons.Right)
{
var hti = dataGrid.HitTest(e.X, e.Y);
dataGrid.ClearSelection();
dataGrid.Rows[hti.RowIndex].Selected = true;
}
か:
私はこれ試してみました右クリックで選択したものをクリックしてください。
おかげで、推測正確な方法です。 – user2396911
あなたは大歓迎です! – Gjeltema