迭代DataGridView只获取前两列的值

时间:2017-02-19 10:26:00

标签: vb.net winforms for-loop datagridview

我有DataGridView有5列。我使用For Next循环来迭代每列的值,但由于某种原因,它仅适用于第1列和第2列。

这是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim a As Decimal
    Dim b As Decimal
    Dim i As Integer
    a = 5.23

    For i = 0 To DataGridView1.Rows.Count - 1

        b = a * (DataGridView1.Item(i, 0).Value)
        MessageBox.Show(b)
    Next
End Sub

enter image description here

1 个答案:

答案 0 :(得分:1)

如果要迭代第一行的每一列,则需要循环到DataGridView.Columns.Count - 1

For i = 0 To DataGridView1.Columns.Count - 1