禁用右键单击DataGridView中的ContextMenuStrip

时间:2012-11-16 11:34:12

标签: c# winforms

我有一个带有dgv1.CellClick的DataGridView。

显示上下文菜单,单击鼠标左键单击该单元格。当发生这种情况时,它还将位置设置为当前单元格,但是当我单击鼠标右键时,contextmenustrip也会显示。我想在单击鼠标右键时禁用或隐藏上下文菜单。

我试过了:

    private void dgv1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
            cms1.Hide();
    }

这不起作用。

有人有任何建议吗?

4 个答案:

答案 0 :(得分:2)

    private MouseButtons e_Button = new MouseButtons();
    private void dgv1_MouseDown(object sender, MouseEventArgs e)
    {
        e_Button = e.Button;
    }

    private void cms1_Opening(object sender, CancelEventArgs e)
    {
        if (e_Button == System.Windows.Forms.MouseButtons.Right)
            e.Cancel = true;
    }

答案 1 :(得分:0)

也许这个:

private void dgv1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)  {
        if ((e.Button != Windows.Forms.MouseButtons.Right)) {
            cms1.Show(datagridview, e.Location);
            }
        }
    }

答案 2 :(得分:0)

试试这个

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.ContextMenu = new ContextMenu();
}

答案 3 :(得分:0)

private void dataGridViewExample_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.ContextMenu = new ContextMenu();
}