如何在Messagebox的Datagridview中单击一个单元格时显示下一个单元格值

时间:2013-12-26 09:21:34

标签: vb.net datagridview messagebox

我需要在vb.net中单击一个单元格时获取下一个单元格的值。例如

ID | NAME | AGE

1 | Azleef | 20

2 | 赛义德 | 22

3 | 吉米 | 23

例如,如果我按 Azleef ,我需要下一个单元格。相应年龄为20

1 个答案:

答案 0 :(得分:1)

您可以依靠CellClick事件获得所需的功能。 DataGridView1的示例代码:

Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    If (e.ColumnIndex > -1 AndAlso e.RowIndex > -1 AndAlso e.ColumnIndex + 1 <= DataGridView1.Columns.Count - 1) Then

        Dim cell As DataGridViewCell = DataGridView1(e.ColumnIndex + 1, e.RowIndex)
        If (cell.Value IsNot Nothing) Then MessageBox.Show(cell.Value.ToString())

    End If

End Sub

此代码适用于每一列(逻辑上除了最后一列);你必须使它适应你想要的列。

相关问题