0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What is the "e" here? What does it do?
@AhmedFaizan Do you know how to deal with events in VB.NET at all? This is one of the arguments of the CellClick event of the DataGridView (but all the events have the two arguments e and sender). Just write this code and will work (you have to adapt the Handles DataGridView1.CellClick part to the name of your datagridview if it is not DataGridView1).
@AhmedFaizan You are welcome (don't feel offended; I am a bit direct some times, but my intention is always helping).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.