![]() |
![]() |
Each group can have a collection of numerical calculations associated with it. Group calculations are represented with a collection of SGCalculation objects. When you add a calculation to a group definition, grid will apply it to records in that group. Result of a calculation can be accessed from code or you can use group formula to show it inside group header or footer.
For example, the following code will calculate a total of the 'Amount' column and show it inside the group footer row:
Dim grp As SGGroup
grp.Calculations.Add sgCalcSum, "Amount"
grp.HeaderTextSource = sgGrpHdrFormula
grp.HeaderFormula = "'Total: ' & GroupCalc(1)"
By default, calculation uses all rows in a group. If you want to skip some rows, you should use conditions. When you create a new calculation with the Add method, you can specify a condition which must be satisfied for a row to be used in a calculation. Following code will calculate total across all items that are not discontinued:
Dim grp As SGGroup
grp.Calculations.Add sgCalcSum, "Amount", "Discontinued", sgOpNot + sgOpEqual, True
grp.HeaderTextSource = sgGrpHdrFormula
grp.HeaderFormula = "'Total: ' & GroupCalc(1)"
Result of a calculation can also be accessed from a code. To access it, use CalculationResult property. In the following example, the FetchGroupFooterText event is used to display custom text in the group footer.
Private Sub SGGrid1_FetchGroupFooterText(ByVal GroupIndex As Integer, _
ByVal RowKey As Long, Text As String)
Dim gh As SGGroupHeading
Set gh = SGGrid1.Rows(RowKey).Parent
Text = "Average: " & _
Round(gh.CalculationResult(3), 2) & " Total: " & _
Round(gh.CalculationResult(4), 2)
End Sub
See Also