如何对数据网格视图行求和.vb.net 中大于 0 的值



>我有这段代码来计算数据网格视图列行中的值总和;

Private Sub GetSUMofUnits()
Dim total As Integer
For Each row As DataGridViewRow In dgvSubjects.Rows
total += row.Cells(3).Value
Next
txtTotalUnits.Text = total
End Sub

但是,我只想对大于 0 的值求和;

例如,这是我的数据网格视图列("单位"(;

Units
3
3
2
-3
Total 8

它应仅计算数据网格视图单元格中大于 0 的值。

我该怎么做?多谢。。。

你可以像这样简单地做到这一点:

Private Sub GetSUMofUnits()
Dim total As Integer
For Each row As DataGridViewRow In dgvSubjects.Rows
If row.Cells(3).Value > 0
total += row.Cells(3).Value
End If
Next
txtTotalUnits.Text = total
End Sub

希望它对:)有所帮助

最新更新