標準のdatagridviewがあり、contextmenustripがあります。私の問題は、ユーザーがマウスの右ボタンをクリックしたときにこのコンテキストメニューストリップを表示する必要があることですが、すべての行に表示する必要はありません。私が選択した行のみ。私はこれを試しました:
dataGridView1.Rows[1].ContextMenuStrip = contextMenuStrip1;
しかし、それは機能しません。
標準のdatagridviewがあり、contextmenustripがあります。私の問題は、ユーザーがマウスの右ボタンをクリックしたときにこのコンテキストメニューストリップを表示する必要があることですが、すべての行に表示する必要はありません。私が選択した行のみ。私はこれを試しました:
dataGridView1.Rows[1].ContextMenuStrip = contextMenuStrip1;
しかし、それは機能しません。
ユーザーが何らかの条件を満たすDataGridViewの列のヘッダーを右クリックした場合、ContextMenuStripを開きたいように思えます。
つまり、DataGridViewMouseDown
イベントを使用し、そのイベントで条件を確認し、条件が満たされている場合はShow
ContextMenuStrip のメソッドを呼び出します。
参照できるコード サンプル:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var ht = dataGridView1.HitTest(e.X, e.Y);
// Place your condition HERE !!!
// Currently it allow right click on last column only
if (( ht.ColumnIndex == dataGridView1.Columns.Count - 1)
&& (ht.Type == DataGridViewHitTestType.ColumnHeader)) {
// This positions the menu at the mouse's location
contextMenuStrip1.Show(MousePosition);
}
}
}