用鼠标右键选择DataGridView单元格或行

DataGridView单元格右键选中
本文介绍了一种在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;
            }
        }

 

转载于:https://www.cnblogs.com/bomb12138/p/3582115.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值