在MouseDown 事件里面写:
private void KnowledgedataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) //判断是不是右键
{
Control control = new Control();
Point ClickPoint = new Point(e.X, e.Y);
control.GetChildAtPoint(ClickPoint);
if (KnowledgedataGridView.HitTest(e.X, e.Y).RowIndex >= 0 && KnowledgedataGridView.HitTest(e.X, e.Y).ColumnIndex >= 0)//判断你点的是不是一个信息行里
{
KnowledgedataGridView.CurrentCell = KnowledgedataGridView.Rows[KnowledgedataGridView.HitTest(e.X, e.Y).RowIndex].Cells[KnowledgedataGridView.HitTest(e.X, e.Y).ColumnIndex];
ContextMenu con = new ContextMenu();
MenuItem menuAddknowledge = new MenuItem("添加知识点");
MenuItem menuLine1 = new MenuItem("-");
MenuItem menuUpdateknowledge = new MenuItem("修改修改知识点");
MenuItem menuLine2 = new MenuItem("-");
MenuItem menuDeleteknowledge = new MenuItem("删除知识点");
menuAddknowledge.Click += new EventHandler(tSButAdd_Click);
menuUpdateknowledge.Click += new EventHandler(tSButUpdate_Click);
menuDeleteknowledge.Click += new EventHandler(tSButDel_Click);
con.MenuItems.Add(menuAddknowledge);
con.MenuItems.Add(menuLine1);
con.MenuItems.Add(menuUpdateknowledge);
con.MenuItems.Add(menuLine2);
con.MenuItems.Add(menuDeleteknowledge);
this.KnowledgedataGridView.ContextMenu = con;
con.Show(KnowledgedataGridView, new Point(e.X + 10, e.Y));
}
}
}