vb.net datadrig视图获取每个单元格值

时间:2010-06-10 11:48:26

标签: vb.net

你好我正在尝试获取datagrid单元格值的第一个值,但它保持循环低谷并返回最后一个值。这是代码:

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

记录例子:

id  name  email
--  ----  -----
1   test  test@test.com

它只返回test@test.com,但我想只得到1

的id

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

每次更新txtoccupier.Text时,您将遍历每行中的所有单元格。这将使其保留最后一个单元格的值。

如果您知道您只希望第一个单元格的值不循环,只需直接访问该单元格的值:

txtoccupier.Text = dgvREcord.CurrentRow.Cells(0).Value.ToString()

答案 1 :(得分:1)

问题是你循环遍历行中的所有单元格,跳过循环并执行txtoccupier.Text = dgvREcord.CurrentRow.Cells(0).Value.ToString()

相关问题