如何在VB.NET中使用空单元格计算Datagridview中的列总和

时间:2018-11-24 02:31:41

标签: vb.net

当列中有一个空单元格时,我没有得到列的总和,但是当没有空单元格时,它就可以正常工作。

我想要的是即使有一个空单元格也要获得一列的总和

这是我的代码:

    Dim rows As Integer = 0
    Dim total_tax_withheld_map As Double

    Try
        Do Until rows = DataGridView4.RowCount
            Dim Tax_Withheld As Double = DataGridView4.Rows(rows).Cells(6).Value
            total_tax_withheld_map = total_tax_withheld_map + Tax_Withheld
            rows = rows + 1
        Loop
    Catch ex As Exception
    End Try

    lblamountmap.Text = total_tax_withheld_map
    Dim dblValuemap As Double = total_tax_withheld_map
    lblamountmap.Text = (dblValuemap.ToString("N", CultureInfo.InvariantCulture))

1 个答案:

答案 0 :(得分:-1)

添加之前,应检查单元格是否为空值 像这样:

Dim rows As Integer = 0
Dim total_tax_withheld_map As Double

Try
    Do Until rows = DataGridView4.RowCount
        If (Not DataGridView4.Rows(rows).Cells(6).Value = Null) Then
            Dim Tax_Withheld As Double = DataGridView4.Rows(rows).Cells(6).Value
            total_tax_withheld_map = total_tax_withheld_map + Tax_Withheld
        End If
        rows = rows + 1
    Loop
Catch ex As Exception
End Try

lblamountmap.Text = total_tax_withheld_map
Dim dblValuemap As Double = total_tax_withheld_map
lblamountmap.Text = (dblValuemap.ToString("N", CultureInfo.InvariantCulture))