vb.net 2008 在鼠标单击DataGridView 某一行时 获取这行的值

我最后这样解决的 】
rows = DataGridView1.CurrentCellAddress.Y.ToString() ' 得到选中行的索引值
ID = CInt(DataGridView1.Rows(rows).Cells(0).Value)
Name = CStr(DataGridView1.Rows(rows).Cells(1).Value)
Desc = CStr(DataGridView1.Rows(rows).Cells(2).Value)
Group = CStr(DataGridView1.Rows(rows).Cells(3).Value)
Date = CStr(DataGridView1.Rows(rows).Cells(4).Value)
这样更简单一点 谢谢!

Private Sub DataGridView1_CurrentCellChanged(By Val sender As Object, By Val e As System.EventArgs) Handles DataGridView1.CurrentCellChanged

//获取选中行第一列的值,也就是第0列的值
Dim result1 As String = DataGridView1.Item(0, DataGridView1.CurrentCell.RowIndex).Value.ToString.Trim

//获取选中行第二列的值,也就是第1列的值
Dim result1 As String = DataGridView1.Item(1, DataGridView1.CurrentCell.RowIndex).Value.ToString.Trim

'......有几列就写几列,如果感觉这样写代码比较累赘,你可以放到for循环里面,把列数用一个变量i代替旧可以了

End sub
温馨提示:答案为网友推荐,仅供参考
相似回答