Originally Posted by
mr_hso
hi
How can I sum a column based on two values in two other columns?
i want add another If on this code
PHP Code:
For Each dgvRow As DataGridViewRow In DataGridView1.Rows
If Not dgvRow.IsNewRow Then
If CDbl(dgvRow.Cells(5).Value) = 25 Then
OverallTotal += CDbl(dgvRow.Cells(19).Value)
End If
End If
You don't need more If statements. In fact, you should have fewer. You can have a single If statement with multiple conditions. That code can be simplified to this:
vb.net Code:
For Each dgvRow As DataGridViewRow In DataGridView1.Rows
If Not dgvRow.IsNewRow AndAlso CDbl(dgvRow.Cells(5).Value) = 25 Then
OverallTotal += CDbl(dgvRow.Cells(19).Value)
End If
Next
If you want to add another condition to that Boolean expression, go right ahead, e.g.
vb.net Code:
For Each dgvRow As DataGridViewRow In DataGridView1.Rows
If Not dgvRow.IsNewRow AndAlso CDbl(dgvRow.Cells(5).Value) = 25 AndAlso CDbl(dgvRow.Cells(6).Value) = 30 Then
OverallTotal += CDbl(dgvRow.Cells(19).Value)
End If
Next