genexus

How to sum (totalize) a variable in grid using for each?


UI'm new in genexus and I'm trying to make a simple sales system. I'm trying to totalize a variable using a For Each line in Grid

Here's my screen:

SaleScreen

1

My SubTotal works but Total stay one step behind

And here's my code:

Event GridProduct.Load
 For Each Product
  &ProductPicture= ProductPicture
  &ProductPrice = ProductPrice
  GridProduct.Load()
 EndFor
Endevent

Event 'BtnPlus'
 &ProductQty= &ProductQty + 1
 Do 'SubTotal'
 Do 'Total'
EndEvent

Event 'BtnMinus'
 &ProductQty= &ProductQty - 1
 Do 'SubTotal'
 Do 'Total'
EndEvent

Event 'SubTotal'
 &ProductSubTotal = &ProductPrice * &ProductQty
 GridProduct.Load()
EndEvent

Event 'Total'
 &ProductTotal = 0
 For Each line in GridProduct
  &ProductTotal = &ProductTotal + &ProductSubTotal 
 EndFor 
 GridProduct.Load()
EndEvent

Solution

  • I solve this problem using 2 situations: To totalize when use button + and -:

    Event 'BtnPlus'
     &ProductQty = &ProductQty + 1
     &ProductTotal = &ProductTotal - &ProductPrice 
     GridProduct.Load()
    EndEvent
    
    Event 'BtnLess'
     &ProductQty = &ProductQty - 1
     &ProductTotal = &ProductTotal - &ProductPrice 
     GridProduct.Load()
    EndEvent
    

    And when the cliente type in &ProductQty

    Event &ProductQty.ControlValueChanging(&ProductQtyValue)
        do 'SubTotal'
        do 'Total'
    EndEvent
    
    Sub 'SubTotal'
     &ProductSubTotal = &ProductPrice * &ProductQty
    EndSub
    
    Sub 'Total'
     &ProductTotal = 0
     For Each line in GridProduct
      &ProductTotal = &ProductTotal + &ProductSubTotal 
     EndFor 
    EndSub