我有一个简单的DataGridView,我试图在单击按钮时获取单元格的值。当它被设置为变量时它是不正确的但是当我快速观察它是正确的。我不知道造成这种情况的原因是什么?
Private Sub btnUpdAll_Click(sender As Object, e As EventArgs) Handles btnUpdAll.Click
For Each r As DataGridViewRow In gvResults.Rows
Dim x = r.Cells(1).Value
Next
End Sub
答案 0 :(得分:0)
为什么要循环,如果你想要一个单元格的值,你选择的单元格。
我会用:
Private Sub btnUpdAll_Click(sender As Object, e As EventArgs) Handles btnUpdAll.Click
Dim row As DataGridViewRow = gvResults.CurrentRow
Dim x as String= row.Cells(e.ColumnIndex).Value.toString()
End Sub
或
Private Sub btnUpdAll_Click(sender As Object, e As EventArgs) Handles btnUpdAll.Click
Dim x As String = gvResults.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
End Sub
答案 1 :(得分:0)
正如@jmcilhinney所说我错误地在循环中声明了变量并且每次都重置它。 @shimrod,我想获得每一行并更新每一行中的单个单元格,如下所示。
Private Sub btnUpdAll_Click(sender As Object, e As EventArgs) Handles btnUpdAll.Click
Dim x
For Each r As DataGridViewRow In gvResults.Rows
x = r.Cells(1).Value
updateRecord(x)
Next
End Sub