The following example groups all files from the windows directory. To try the example, place a SGGrid on a form. Paste the code into the Declarations section and press F5.
Private Sub Form_Load()
Dim sFile As String
Dim sgCol As SGColumn
Dim SGGroup As SGGroup
Dim sWinDir As String
Dim dtFileDate As Date
Dim i As Integer
'change default groups style
With SGGrid1
.Styles("Heading").BackColor = RGB(165, 142, 107)
.Styles("Selection").BackColor = RGB(181, 154, 115)
.Styles("InactiveSelection").BackColor = RGB(132, 190, 173)
.Styles("GroupHeader").BackColor = RGB(231, 203, 123)
.BackColor = RGB(214, 207, 189)
.SpecialMode = sgModeListBox
End With
'disable redraw to speed up proces
SGGrid1.RedrawEnabled = False
'remove all columns
SGGrid1.Columns.RemoveAll True
'ColumnClickSort needs CacheAllRecords
SGGrid1.CacheAllRecords = True
SGGrid1.ColumnClickSort = True
SGGrid1.GroupByBoxVisible = False
'Column creations
Set sgCol = SGGrid1.Columns.Add("File")
sgCol.Caption = "File name"
sgCol.Width = 2200
Set sgCol = SGGrid1.Columns.Add("FileDate")
sgCol.Caption = "File date"
sgCol.Width = 2200
sgCol.DataType = sgtDateTime
sgCol.SortType = sgSortTypeDateTime
Set sgCol = SGGrid1.Columns.Add("Size")
sgCol.Caption = "Size"
sgCol.DataType = sgtLong
Set sgCol = SGGrid1.Columns.Add("FileYear")
sgCol.Hidden = True
sgCol.Caption = "File year"
Set sgCol = SGGrid1.Columns.Add("FileMonth")
sgCol.Hidden = True
sgCol.Caption = "File month"
sWinDir = Environ("WINDIR") & "\"
sFile = Dir(sWinDir & "*.*", vbNormal)
'add files to the grid
Do Until Len(sFile) = 0
i = i + 1
SGGrid1.DataRowCount = i
SGGrid1.Rows.At(i).Cells(0).Value = sFile
dtFileDate = FormatDateTime(FileDateTime(sWinDir & sFile), vbShortDate)
SGGrid1.Rows.At(i).Cells(1).Value = dtFileDate
SGGrid1.Rows.At(i).Cells(2).Value = FileLen(sWinDir & sFile)
SGGrid1.Rows.At(i).Cells(3).Value = Year(dtFileDate)
SGGrid1.Rows.At(i).Cells(4).Value = Month(dtFileDate)
sFile = Dir
Loop
'group grid by year and month
Set SGGroup = SGGrid1.Groups.Add("FileYear", sgSortAscending, _
sgSortTypeNumber, True, False)
Call SGGrid1.Groups.Add("FileMonth", sgSortAscending, _
sgSortTypeNumber, False, False)
'show formula result in header and footer
SGGroup.HeaderTextSource = sgGrpHdrFormula
SGGroup.FooterTextSource = sgGrpFooterFormula
'create calculations for header and footer
SGGroup.Calculations.Add sgCalcSum, "Size"
SGGroup.Calculations.Add sgCalcAverage, "Size"
'create formula
SGGroup.HeaderFormula = "'Total size for ' & GroupingValue & ' is ' & Format((GroupCalc(1) / 1024),'Standard') & ' KB (' & TotalChildCount & ' files)'"
SGGroup.FooterFormula = "'The average file size for ' & GroupingValue & ' is ' & Format((GroupCalc(2) / 1024),'Standard') & ' KB'"
Set SGGroup = Nothing
'the argument doRefresh in the Groups.Add method is false so
'grouping need refresh
SGGrid1.RefreshGroups
'redrow grid
SGGrid1.RedrawEnabled = True
End Sub
Back to topic