是否可以在DataGridView WindowsForms C#中添加ComboBox作为ContextMenu项? [英] Is it possible to add ComboBox as ContextMenu item in DataGridView WindowsForms C#?

查看:137
本文介绍了是否可以在DataGridView WindowsForms C#中添加ComboBox作为ContextMenu项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户右键单击时在数据网格上向ContextMenu添加一个组合框,菜单会弹出。它应该显示退出并分配给bankID菜单选项,其中银行ID是组合框下拉列表。我做了一些在线搜索,但找不到任何让我惊讶的东西,甚至可能做到这一点。我创建了一个模拟图像,显示了我正在尝试使用某些代码。如果有人做了类似的事情并与我分享,我会非常感激。谢谢。





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 =退出;

menuOpp.MenuItems.Add(退出,新的EventHandler(menuItem_Click));

cmbAssign.Items.Add(assign);



dataGridView2.ContextMenu = menuOpp;



currentMouseOverRow = dataGridView2.HitTest(eX,eY).RowIndex;



menuOpp.Show(dataGridView2,new Point(eX,eY));

}

}

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

推荐答案

嗯...我是我在这里寻找一些东西?

如果你向表单中添加一个标准的ContextMenuStrip,并将它分配给你的DataGridView的ContextMenuStrip属性,你只需点击它就可以在菜单中添加任意数量的组合框。在在此处输入框的右侧向下箭头,从列​​表中选择ComboBox。



那么这是一个问题的哪一部分?
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?


我能够自己解决这个问题。我创建了一个ContextMenuStrip,添加了一个带有子ComboBox项的MenuItem,在ToolStripMenuItem Click事件中填充了ComboBox。然后将ContextMenuStrip对象分配给其ContextMenuStrip属性。将ContextMenuStrip对象分配给控件后,上下文菜单显示为用户右键单击。



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.

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


这篇关于是否可以在DataGridView WindowsForms C#中添加ComboBox作为ContextMenu项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆