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