The following example fills the grid array with random numbers. It creates two groups, by odd/even numbers and by mod of seven. It uses the GroupHeadings property to create the group header caption. Paints group headers using the FetchGroupHeaderStyle event. Use the CommandButton to remove the current row. To try the example, place SGGrid and CommandButton control on a form. Paste the code into the Declarations section, press F5 and click button.
Private Sub Command1_Click()
Dim lKey As Long
If SGGrid1.DataRowCount = 0 Then Exit Sub
lKey = SGGrid1.Rows.Current.Key
Select Case SGGrid1.Rows.Current.Type
Case sgGroupHeader
SGGrid1.GroupHeadings.RemoveGroup lKey
Case sgSimpleRow
SGGrid1.Rows.Remove lKey
End Select
End Sub
Private Sub SGGrid1_FetchGroupHeaderData _
(ByVal GroupIndex As Integer, ByVal RowKey As Long, _
Text As String, PictureExpanded As Variant, _
PictureCollapsed As Variant)
Dim lCount As Long, gh As SGGroupHeading
If RowKey = SGGrid1.GroupHeadings(1).Row.Key Then
For Each gh In SGGrid1.GroupHeadings(1).Headings
lCount = lCount + gh.ChildRows.Count
Next
Text = "There are " & lCount & " even numbers"
Else
For Each gh In SGGrid1.GroupHeadings(2).Headings
lCount = lCount + gh.ChildRows.Count
Next
Text = "There are " & lCount & " odd numbers"
End If
End Sub
Private Sub SGGrid1_FetchGroupHeaderStyle _
(ByVal GroupIndex As Integer, ByVal RowKey As Long, _
ByVal HeaderStyle As IsgStyle)
With HeaderStyle
.BackColor = QBColor(SGGrid1.Rows(RowKey).Cells(0).Value)
.ForeColor = vbWhite
.TextAlignment = sgAlignCenterCenter
.Font.Bold = True
End With
End Sub
Private Sub SGGrid1_OnInit()
Dim ar As SGArray, i As Integer
Dim lRndValue As Long
SGGrid1.Columns.RemoveAll
SGGrid1.FitLastColumn = True
SGGrid1.GroupByBoxVisible = False
With SGGrid1.Columns.Add("EvenOdd")
.Hidden = True
End With
With SGGrid1.Columns.Add("ModSeven")
.Hidden = True
End With
With SGGrid1.Columns.Add("Number")
.Caption = "Number"
.HeadingStyle.TextAlignment = sgAlignCenterCenter
.Style.TextAlignment = sgAlignCenterCenter
.Style.Format = Chr(34) & "---> " & Chr(34) & "#,##0" & _
Chr(34) & " <---" & Chr(34)
End With
SGGrid1.DataRowCount = 120
Set ar = SGGrid1.Array
Randomize
For i = 0 To SGGrid1.DataRowCount - 1
lRndValue = CLng(Rnd * 120000)
ar.Value(i, 0) = lRndValue Mod 2
ar.Value(i, 1) = lRndValue Mod 7
ar.Value(i, 2) = lRndValue
Next
SGGrid1.Groups.Add "EvenOdd", sgSortAscending, _
sgSortTypeNumber, False, False
SGGrid1.Groups(1).HeaderTextSource = sgGrpHdrFireFetchText
With SGGrid1.Groups.Add("ModSeven", sgSortAscending, _
sgSortTypeNumber, False, False)
.FetchHeaderStyle = True
.HeaderTextSource = sgGrpHdrFormula
.HeaderFormula = "GroupingValue"
End With
SGGrid1.RefreshGroups
SGGrid1.CollapseAll
Set ar = Nothing
End Sub
Back to topic