在 DataGridView 上右键单击显示子菜单 - C#

Show Sub Menu on right Click On DataGridView - C#

您好,我在 datagridview 上添加了一个右键单击事件,该事件显示菜单,但我想在单击或鼠标悬停在 menuItem

之一上时显示子菜单
protected void datagridview1_MouseClick(object sender, MouseEventArgs  e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenuStrip my_menu = new ContextMenuStrip();
            int position_xy_mouse_row = dataGridView1.HitTest(e.X, e.Y).RowIndex;

            if (position_xy_mouse_row >= 0)
            {
                my_menu.Items.Add("Show Details").Name = "Details";

                if ((SecurityLevel == "something" && dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value1") || (SecurityLevel == "something" && dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value2"))
                {
                    my_menu.Items.Add("update").Name = "Update ";
                    if (dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value1")
                    {
                        MenuItem updateStatus = new MenuItem("Update");
                        updateStatus.MenuItems.Add(new MenuItem("somevalue").Name = "somevalue");
                        updateStatus.MenuItems.Add(new MenuItem("someothervalue").Name = "someothervalue");
                    }
                    if (dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value2")
                    {
                        MenuItem updateStatus = new MenuItem("Update");
                        updateStatus.MenuItems.Add(new MenuItem("someothervalue").Name = "someothervalue");
                        updateStatus.MenuItems.Add(new MenuItem("newvalue").Name = "newvalue");
                    }
                }
            }
            my_menu.Show(dataGridView1, new Point(e.X, e.Y));
            my_menu.ItemClicked += new ToolStripItemClickedEventHandler(my_menu_ItemClicked);
        }
    }

但我只能在我的菜单上看到 2 个菜单项,即 Show DetailsUpdate,我看不到要更新的子菜单。

要添加子菜单项,您需要将所需的项转换为 ToolStripMenuItem,然后将子项添加到 DropDownItems 属性 - 连接事件构造函数中的处理程序。您可以使用唯一的事件处理程序创建每个事件处理程序,也可以只创建一个对 ToolStripMenuItem.Name:

进行逻辑检查的处理程序
if (position_xy_mouse_row >= 0)
{
    my_menu.Items.Add("Show Details").Name = "Details";

    if ((SecurityLevel == "something" && dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value1") || (SecurityLevel == "something" && dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value2"))
    {
        my_menu.Items.Add("update").Name = "Update";
        if (dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value1")
        {
            (my_menu.Items["Update"] as ToolStripMenuItem).DropDownItems.Add("somevalue", null, new EventHandler(this.SubItem_Click)).Name = "somevalue";
            (my_menu.Items["Update"] as ToolStripMenuItem).DropDownItems.Add("someothervalue", null, new EventHandler(this.SubItem_Click)).Name = "someothervalue";
        }
        if (dataGridView1.SelectedRows[0].Cells[8].Value.ToString() == "value2")
        {
            (my_menu.Items["Update"] as ToolStripMenuItem).DropDownItems.Add("someothervalue", null, new EventHandler(SubItem_Click)).Name = "someothervalue";
            (my_menu.Items["Update"] as ToolStripMenuItem).DropDownItems.Add("newvalue", null, new EventHandler(SubItem_Click)).Name = "newvalue";
        }
    }
}
my_menu.Show(dataGridView1, new Point(e.X, e.Y));
my_menu.ItemClicked += new ToolStripItemClickedEventHandler(MenuItem_Clicked);

那么你的处理人员:

private void MenuItem_Clicked(object sender, ToolStripItemClickedEventArgs e)
{
  Console.WriteLine("Clicked {0}", e.ClickedItem.Name);
}

private void SubItem_Click(object sender, EventArgs e)
{
  Console.WriteLine("Clicked {0}", (sender as ToolStripMenuItem).Name);
}