Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to add a combobox to ContextMenu on a datagrid when a user right clicks, the menu will pop up. It should show exit and assign to bankID menu options where the bank ID is a combobox dropdown. I did some online search but couldn't find anything close which makes me wonder, it is even possible to do that. I created a mockup image of what I'm trying to do with some code. If anyone has done something similar and share with me, I would really appreciate it. Thanks.


private ContextMenu menuOpp = new ContextMenu();
private ComboBox cmbAssign = new ComboBox();
private int currentMouseOverRow;

private void dataGridView2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
menuOpp.Name = "Exit";
menuOpp.MenuItems.Add("Exit", new EventHandler(menuItem_Click));
cmbAssign.Items.Add("assign");

dataGridView2.ContextMenu = menuOpp;

currentMouseOverRow = dataGridView2.HitTest(e.X, e.Y).RowIndex;

menuOpp.Show(dataGridView2, new Point(e.X, e.Y));
}
}
Posted

Um...am I missing something here?
If you add a standard ContextMenuStrip to your form, and assign it to the ContextMenuStrip property of your DataGridView, you can add as many combo boxes as you like to the menu just by clicking the down arrow at the right of the "Type Here" box and selecting "ComboBox" from the list.

So what part of this is a problem?
 
Share this answer
 
I was able to resolve this myself. I created a ContextMenuStrip, added a MenuItem with sub ComboBox item, populated the ComboBox on ToolStripMenuItem Click event. Then assigned ContextMenuStrip object to its ContextMenuStrip property. After assigning the ContextMenuStrip object to a control, the contextual menu displayed as user right click.

C#
<pre>private void InitializeComponent()
{
	System.Windows.Forms.ContextMenuStrip Context =
		new System.Windows.Forms.ContextMenuStrip();

	ToolStripMenuItem mnuAssign = new ToolStripMenuItem("Assign");

	Context.Items.Add(mnuAssign);
	ContextMenuStrip = context;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900