hello i’m trying to get the first value of a datagrid cell value but it keeps looping trough and returns the last value. here is the code:
Dim cell As DataGridViewCell
txtoccupier.Text = ""
Try
For Each cell In dgvREcord.CurrentRow.Cells()
txtoccupier.Text = cell.Value.ToString
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Try
End Try
record eample:
id name email
-- ---- -----
1 test test@test.com
it returns only test@test.com but i want to get only the id which is 1
thanks for your help
You’re looping over all of the cells in each row updating
txtoccupier.Texteach time. This will leave it holding the value of the last cell.If you know you only want the value of the first cell don’t loop, just access that cell’s value directly: