现在的位置: 首页 > 综合 > 正文

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

2013年02月05日 ⁄ 综合 ⁄ 共 684字 ⁄ 字号 评论关闭
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的关联右键菜单去掉。不要在设计器中进行设计。

抱歉!评论已关闭.