tags:

views:

20

answers:

2

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  [email protected]

it returns only [email protected] but i want to get only the id which is 1

thanks for your help

+1  A: 

You're looping over all of the cells in each row updating txtoccupier.Text each 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:

txtoccupier.Text = dgvREcord.CurrentRow.Cells(0).Value.ToString()
ChrisF
thank you very much
Gbolahan
+1  A: 

The problem is that you're looping through all the cells in the row, skip the loop and just do txtoccupier.Text = dgvREcord.CurrentRow.Cells(0).Value.ToString()

ho1
you guys are the men..lol
Gbolahan