简体   繁体   English

是否可以在DataGridView WindowsForms C#中将ComboBox添加为ContextMenu项?

[英]Is it possible to add ComboBox as ContextMenu item in DataGridView WindowsForms C#?

![enter image description here][1]I am trying to add a combobox to ContextMenu on a datagrid when a user right clicks, the menu will pop up. ![在此处输入图像描述] [1]我试图在用户右键单击时将组合框添加到数据网格的ContextMenu上,然后弹出菜单。 It should show exit and assign to bankID menu options where the bank ID is a combobox dropdown. 它应该显示退出并分配给bankID菜单选项,其中bank ID是组合框下拉菜单。 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));
        }
    }

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. 我创建了一个ContextMenuStrip,添加了一个带有子ComboBox项的MenuItem,并在ToolStripMenuItem Click事件上填充了ComboBox。 Then assigned ContextMenuStrip object to its ContextMenuStrip property. 然后将ContextMenuStrip对象分配给其ContextMenuStrip属性。 After assigning the ContextMenuStrip object to a control, the contextual menu displayed as user right click. 将ContextMenuStrip对象分配给控件后,上下文菜单显示为用户右键单击。

private void InitializeComponent()
{
    System.Windows.Forms.ContextMenuStrip Context =
            new System.Windows.Forms.ContextMenuStrip();

    ToolStripMenuItem mnuAssign = new ToolStripMenuItem("Assign");

    Context.Items.Add(mnuAssign);
    ContextMenuStrip = context;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM