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

时间:2016-03-23 20:59:32

标签: c# datagridview mouseevent right-click contextmenustrip

您好我在我的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);
        }
    }

但我的菜单上只能看到Show DetailsUpdate的2个菜单项,我无法看到子菜单进行更新。

1 个答案:

答案 0 :(得分:2)

要添加子菜单项,您需要将所需项目强制转换为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);
}