用鼠标右键选择DataGridView单元格或行
在datagirdview_cellmousedown事件中先将CurrentCell(或CurrentRow)的Selected属性设为false,然后将鼠标右键点击的单元格(或行)设为CurrentCell(或CurrentRow),最后再将CurrentCell(或CurrentRow)的Selected属性设为true。
参考代码如下:
//右键可选中单元格 private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right && e.Clicks == 1) { dataGridView1.CurrentCell.Selected = false; dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; dataGridView1.CurrentCell.Selected = true; } }
作者:尼克劳斯
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号