I'm writing a C# (.NET 3.5) app with a usercontrol that inherits from DataGridView. When the user right-clicks a column header, I want to display a context menu.
I've actually got this working find on 2 forms in my app. I'm stumped because the ContextMenu won't show on the same control on a third form. I do see that the Popup event gets fired, but I don't see the menu being drawn on the screen, and none of the menu item event handlers are getting called.
I have absolutely no idea why my context menu isn't being drawn, and it's driving me batty.
Unfortunately, my code is too complex to post all of it here... I'm including the short section where I build and display the menu. I'm not convinced the problem is in this code, but I don't know where else it would be.
if (hti.RowIndex == -1)
{
ClickedColumnHeader = this.Columns[hti.ColumnIndex];
//Build a context menu and show it.
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Clear();
MenuItem mnuHide = new MenuItem("Hide");
mnuHide.Click += new EventHandler(mnuHide_Click);
MenuItem mnuRename = new MenuItem("Rename...");
mnuRename.Click += new EventHandler(mnuRename_Click);
MenuItem mnuCurrencyFormat = new MenuItem("Format as Currency");
mnuCurrencyFormat.Checked = false;
if (this.Columns[hti.ColumnIndex].DefaultCellStyle.Format == "c")
{
mnuCurrencyFormat.Checked = true;
}
mnuCurrencyFormat.Click += new EventHandler(mnuCurrencyFormat_Click);
MenuItem mnuSeparator = new MenuItem("-");
MenuItem mnuShow = new MenuItem("Show");
foreach (DataGridViewColumn col in this.Columns)
{
if (col.Visible == false)
{
MenuItem x = new MenuItem(col.HeaderText);
x.Click += new EventHandler(x_Click);
mnuShow.MenuItems.Add(x);
}
}
mnu.MenuItems.Add(mnuHide);
mnu.MenuItems.Add(mnuRename);
mnu.MenuItems.Add(mnuCurrencyFormat);
mnu.MenuItems.Add(mnuSeparator);
mnu.MenuItems.Add(mnuShow);
//for debugging...
mnu.Popup += new EventHandler(mnu_Popup);
mnu.Collapse += new EventHandler(mnu_Collapse);
mnu.Show(this, new System.Drawing.Point(f.X, f.Y));
}
I'd really appreciate any assistance the community could offer. I'm really hoping I'm just doing something dumb here.