Question

I got this part of code, where I open contextMenuStrip in respond to mouse right-click (over dataGridView table).

The problem is, that the FIRST TIME i right click - the menu doesn't pop up. On the second time it pops up, and since then everything is working well..

    private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y); //get info
        int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;

        if (e.Button == MouseButtons.Right) //MouseButton right: Open context menu strip.
         {
             dataGridView1.Rows[currentMouseOverRow].Selected = true; //Select the row

             ContextMenuStrip Menu = new ContextMenuStrip();
             ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Delete it");
             MenuOpenPO.Click += new EventHandler(MenuOpenPO_Click);
             Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
             dataGridView1.ContextMenuStrip = Menu; //Assign to dataGridView1
         }
    }

Any help ? :) I use visual studio 2012.

Was it helpful?

Solution

Problem : You have added ContextMenu to the DataGridView after RightClick event. so ContextMenu willbe added to your DataGridView after the first RightClick hence user can see the attached ContextMenu from the further RightClick events.

Solution : you need to add the ContextMenu before right clicking on the DataGridView so that it will be appeared for each RightClick event.

Note : if ContextMenu is assigned to any control , it willbe displayed on rightclick by default, means you do not need to add it for each RightClick event on the control explicitly.

Try This: in Form Load Event

   private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        ContextMenuStrip Menu = new ContextMenuStrip();
        ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Delete it");
        MenuOpenPO.Click += new EventHandler(MenuOpenPO_Click);
        Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
        dataGridView1.ContextMenuStrip = Menu; //Assign to dataGridView1
    }

OTHER TIPS

I typically create a ContextMenuStrip object of my own (private variable) and Show() it when needed. I suspect that it's being assigned the first time, but cannot display because the newly added menu doesn't get its own right-click trigger to display. That only happens the second time, once it exists.

Like this: private ContextMenuStrip _myMenu;

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y); //get info
    int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;

    if (e.Button == MouseButtons.Right) //MouseButton right: Open context menu strip.
     {
         dataGridView1.Rows[currentMouseOverRow].Selected = true; //Select the row

         _myMenu = new ContextMenuStrip();
         ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Delete it");
         MenuOpenPO.Click += new EventHandler(MenuOpenPO_Click);
         _myMenu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
         _myMenu.Show(new Point(e.X, e.Y));
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top