datagridview右键菜单鼠标当前所在数据行进行选中,并在无数据时不显示右键菜单。

View Code
 1         private void DgvConsignmentBills_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
 2         {
 3             if (e.Button == MouseButtons.Right)
 4             {
 5                 if (e.RowIndex >= 0)
 6                 {
 7                     this.DgvConsignmentBills.ClearSelection();
 8                     this.DgvConsignmentBills.Rows[e.RowIndex].Selected = true;
 9                     this.DgvConsignmentBills.CurrentCell = this.DgvConsignmentBills.Rows[e.RowIndex].Cells[e.ColumnIndex];
10                     contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
11                 }
12             } 

注意:使用次代码需要江datagridview的关联右键菜单去掉。不要在设计器中进行设计。

原文地址:https://www.cnblogs.com/angels/p/2226623.html