我们如何添加datagridview的多个单元格的数值?

时间:2013-08-24 10:03:10

标签: .net winforms datagridview

enter image description here

我创建了一个用于制作帐单的数据网格视图。现在我要添加所有列4的值。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

 Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If e.ColumnIndex = 1 Then  // here your columnindex
            calctotal()
        End If
    End Sub
    Private Sub calctotal()
        Dim tot As Single
        For Each rw As DataGridViewRow In DataGridView1.Rows
            If rw.Cells(0).Value <> "" Then
                If String.IsNullOrEmpty(tot) Then
                    tot = Val(rw.Cells(0).Value) * Val(rw.Cells(1).Value)
                Else
                    tot = tot + Val(rw.Cells(0).Value) * Val(rw.Cells(1).Value)
                End If
                Label1.Text = tot
            End If
        Next
    End Sub