The following example creates tree columns: Month, Day and Amount and fills them with random data. It uses Month column to create group object and creates four group's calculations. It uses the FetchGroupFooterText and FetchGroupHeaderData events to display result of calculations in the group's footer and header. To try the example, place BeeGrid on a form. Paste the code into the Declarations section and press F5.
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.Row.GroupHeading
Text = "Average: " & _
Round(gh.CalculationResult(3), 2) & " Total: " & _
Round(gh.CalculationResult(4), 2)
End Sub
Private Sub SGGrid1_FetchGroupHeaderData _
(ByVal GroupIndex As Integer, ByVal RowKey As Long, _
Text As String, PictureExpanded As Variant, _
PictureCollapsed As Variant)
Dim gh As SGGroupHeading
Set gh = SGGrid1.Rows(RowKey).GroupHeading
Text = MonthName(gh.GroupingValue) & " from " & _
gh.CalculationResult(1) & " to " & gh.CalculationResult(2)
End Sub
Private Sub SGGrid1_OnInit()
Dim ar As SGArray, dtDay As Date
Dim i As Integer, lFirstDay As Long
Dim grp As SGGroup
'create columns and rows
SGGrid1.DataColCount = 0
SGGrid1.DataRowCount = 100
SGGrid1.FitLastColumn = True
With SGGrid1.Columns.Add("Month")
.DataType = sgtShort
.Hidden = True
End With
With SGGrid1.Columns.Add("Day")
.DataType = sgtDateTime
.Width = .Width * 2
End With
With SGGrid1.Columns.Add("Amount")
.DataType = sgtCurrency
End With
'add sample data
Set ar = SGGrid1.Array
lFirstDay = CLng(DateSerial(Year(Now), 1, 1))
For i = 0 To ar.RowCount - 1
'get random date
dtDay = CDate(lFirstDay + 365 * Rnd)
ar.Value(i, 0) = Month(dtDay)
ar.Value(i, 1) = FormatDateTime(dtDay, vbShortDate)
ar.Value(i, 2) = Round((Rnd * 1000), 2)
Next
Set ar = Nothing
'create group and calculation
Set grp = SGGrid1.Groups.Add("Month", _
sgSortAscending, sgSortTypeNumber, True, False)
'use formulas for header and footer text
grp.HeaderTextSource = sgGrpHdrFireFetchText
grp.FooterTextSource = sgGrpFooterFireFetchText
'create calculations
grp.Calculations.Add sgCalcMinimum, "Day"
grp.Calculations.Add sgCalcMaximum, "Day"
grp.Calculations.Add sgCalcAverage, "Amount"
grp.Calculations.Add sgCalcSum, "Amount"
Set grp = Nothing
SGGrid1.RefreshGroups sgCollapseGroups
End Sub
Back to topic