How to get values from the selected Row in a DataTable on CellClick Event

4010 views vb.net
5

I just want to ask on how can I get the values from a row in DataTable

ex.:

DataTable:

col1 | col2 | col3 | col4
-------------------------
c1r1 | c2r1 | c3r1 | c4r1

c1r2 | c2r2 | c3r2 | c4r2

In the DataGridView, col2 is hidden for display purposes. col2 holds the ID values which is not included in the display.
So if I click, lets say, c4r1 in the DataGridView I can get the value of col2 which is c2r1 and put in a Label.

I've search on how to do it but I can't get the result I want.

answered question

1 Answer

11

Subscribe to the DataGridView.RowEnter event.
There, update a Label.Text property with the Value of the Column(1) Cell in the current Row :

Private Sub dataGridView1_RowEnter(sender As Object, e As DataGridViewCellEventArgs)
    Label1.Text = dataGridView1(1, e.RowIndex).Value.ToString()
    'Or...
    'Label1.Text = dataGridView1("col2", e.RowIndex).Value.ToString()
End Sub

posted this

Have an answer?

JD

Please login first before posting an answer.