DataGridView实现点击左边空白部分选中行,并对其进行操作

本文介绍了如何在VB.NET的DataGridView控件中,通过监听RowHeaderMouseClick事件来实现点击行的空白部分选中行,并获取选中行的班级、姓名和手机信息,解决了CellContentClick事件会导致需要点击单元格拉伸才能触发的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VS2005对控件DataGridView,双击控件时,默认的函数头是:

Private Sub dg1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg1.CellContentClick

Dim index As Integer
        index = DataGridView1.CurrentCell.RowIndex
        If DataGridView1.Rows(index).Selected = True Then

            banji.Text = DataGridView1.CurrentRow.Cells("班级").Value.ToString
            xingming.Text = DataGridView1.CurrentRow.Cells("姓名").Value.ToString

            shouji.Text = DataGridView1.CurrentRow.Cells("手机").Value.ToString
        End If

End Sub

如果用这个方法实现题目所需功能,则有所偏差,出现的效果是:点击单元格的拉伸才会进行操作。

最终解决方法是用如下函数:

 Private Sub dg1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dg1.RowHeaderMouseClick

Dim index As Integer
        index = DataGridView1.CurrentCell.RowIndex
        If DataGridView1.Rows(index).Selected = True Then

            banji.Text = DataGridView1.CurrentRow.Cells("班级").Value.ToString
            xingming.Text = DataGridView1.CurrentRow.Cells("姓名").Value.ToString

            shouji.Text = DataGridView1.CurrentRow.Cells("手机").Value.ToString
        End If

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值