1
您好我加右擊事件對我datagridview
,顯示菜單,但我想顯示點擊時子菜單或鼠標懸停在的menuItem
顯示子菜單 - C#
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 Details
和Update
,我看不到用於更新的子菜單。
我看不出有任何的代碼添加菜單項'updateStatus'到'my_menu' –