0
votes

I have a DataGridView with a dgv1.CellClick.

The context menu shows up with the cell is clicked with the left mouse button. When this happens it also sets the position to the current cell BUT the contextmenustrip also shows up when I click on the RIGHT mouse button. I want to disable or hide the context menu when the right mouse button is clicked.

I have tried:

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

and this did not work.

Does anyone have any advice?

4

4 Answers

3
votes
    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;
    }
0
votes

Maybe with this:

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

Try this

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