Question

I need to get the value of the next cell when one cell is clicked in vb.net.For example

ID | NAME | AGE

1 | Azleef | 20

2 | Saeed | 22

3 | Jimmy | 23

For example if I press Azleef I need the next cell. The corresponding age which is 20

Was it helpful?

Solution

You can get the functionality you want by relying on the CellClick event. Sample code for 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

This code works with every column (except, logically, with the last one); you have to adapt it to work just with the column(s) you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top