Scott
Scott

Reputation: 53

Disable Right-Click for ContextMenuStrip in DataGridView

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?

Upvotes: 0

Views: 7004

Answers (4)

Scott
Scott

Reputation: 53

    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;
    }

Upvotes: 3

Obama
Obama

Reputation: 2612

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

Upvotes: 0

Nag
Nag

Reputation: 689

Try this

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

Upvotes: 0

Carlos Landeras
Carlos Landeras

Reputation: 11063

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);
            }
        }
    }

Upvotes: 0

Related Questions