![](/img/trans.png)
[英]How to remove a row from DataGridView if data within Total column is Zero
[英]How to get the total of a column in DataGridView
每次有一個空白單元格時結果為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
就像Mat所說的一樣,但我更喜歡.net方式。 他們說TryParse
更快,如果您有很多行,可能會有所作為。
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
如果單元格不包含任何值,則拋出InvalidCastException
並保留Try
-block。
因此,您需要在計算之前檢查單元格中的數字值。 此外,您可以縮短代碼並省略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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.