PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2015 sum with 3 condition-VBForums
Results 1 to 5 of 5

Thread: sum with 3 condition

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    2

    sum with 3 condition

    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 

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,423

    Re: sum with 3 condition

    Use Val instead of CDbl... It avoids conversion errors better

    Code:
    If Not dgvRow.IsNewRow Then 
        If Val(dgvRow.Cells(5).Value) = 25 Then 
            if whatever = something then
                OverallTotal += Val(dgvRow.Cells(19).Value) 
            End If 
        End If 
    End If

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    2

    Resolved Re: sum with 3 condition

    Quote Originally Posted by .paul. View Post
    Use Val instead of CDbl... It avoids conversion errors better

    Code:
    If Not dgvRow.IsNewRow Then 
        If Val(dgvRow.Cells(5).Value) = 25 Then 
            if whatever = something then
                OverallTotal += Val(dgvRow.Cells(19).Value) 
            End If 
        End If 
    End If



    Thank you for helping me

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: sum with 3 condition

    Quote Originally Posted by mr_hso View Post
    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:
    1. For Each dgvRow As DataGridViewRow In DataGridView1.Rows
    2.     If Not dgvRow.IsNewRow AndAlso CDbl(dgvRow.Cells(5).Value) = 25 Then
    3.         OverallTotal += CDbl(dgvRow.Cells(19).Value)
    4.     End If
    5. Next
    If you want to add another condition to that Boolean expression, go right ahead, e.g.
    vb.net Code:
    1. For Each dgvRow As DataGridViewRow In DataGridView1.Rows
    2.     If Not dgvRow.IsNewRow AndAlso CDbl(dgvRow.Cells(5).Value) = 25 AndAlso CDbl(dgvRow.Cells(6).Value) = 30 Then
    3.         OverallTotal += CDbl(dgvRow.Cells(19).Value)
    4.     End If
    5. Next

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,423

    Re: sum with 3 condition

    What jm showed you is an alternative and more succinct way of writing the same logic. You can have as many if statements, or as few if statements as you want...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width