Reputation: 1
Every time there is a blank cell the result is 0?
Dim tot As Decimal
Try
If BREAKDOWNLIST.RowCount <> 1 Then
For Each row As DataGridViewRow In BREAKDOWNLIST.Rows
If IsNothing(Me.BREAKDOWNLIST.CurrentRow.Cells(5).Value) Then
Else
If BREAKDOWNLIST.RowCount = 1 Then
tot = Val(row.Cells(5).Value)
ElseIf BREAKDOWNLIST.RowCount > 1 Then
tot += Val(row.Cells(5).Value)
End If
End If
Next
ElseIf BREAKDOWNLIST.RowCount = 1 Then
End If
TOTAL.Text = tot
Catch
End Try
Upvotes: 0
Views: 3582
Reputation: 15091
Like Mat said but I like the .net way better. They say TryParse
is faster which could make a difference if you have an awful lot of rows.
Dim decValue As Decimal
Dim tot As Decimal
For Each row As DataGridViewRow In BREAKDOWNLIST.Rows
If Decimal.TryParse(row.Cells(5).Value, decValue) Then
tot += decValue
End If
Next
Upvotes: 1
Reputation: 7517
If the cell contains no value, an InvalidCastException
is thrown and the Try
-block is left.
So you'll need to check the cell for a numeric value before calculating.
Furthermore you can shorten your code and omit the Try/Catch
-block:
Dim tot As Decimal
For Each row As DataGridViewRow In BREAKDOWNLIST.Rows
If IsNumeric(row.Cells(5).Value) Then
tot += Val(row.Cells(5).Value)
End If
Next
TOTAL.Text = tot.ToString()
Upvotes: 0