2010-06-10 74 views
0

你好我想获取datagrid单元格值的第一个值,但它保持循环低谷并返回最后一个值。这里是代码:vb.net datadrig视图获得每个单元格的值

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 

记录eample:

id name email 
-- ---- ----- 
1 test [email protected] 

[email protected]的回报,但我想只有ID为1

感谢您的帮助

+0

嗨,请编辑你的问题,以便代码格式化,以便我们可以更容易地阅读。使用代码按钮(从左边5)或每行前面放置4个空格。 – uriDium 2010-06-10 11:53:14

回答

1

您正在循环遍历每行更新txtoccupier.Text的所有单元格。这将使其保持最后一个单元格的值。

如果你知道你只想要第一个单元格的值不循环,只是访问该单元格的值直接:

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

非常感谢你 – Gbolahan 2010-06-10 12:23:00

1

的问题是,你通过该行中的所有单元格循环,跳过循环,只是做txtoccupier.Text = dgvREcord.CurrentRow.Cells(0).Value.ToString()

+0

你们是男人..大声笑 – Gbolahan 2010-06-10 13:09:13

相关问题