Public Class frmDatagridviewGrouper
Dim bs As New BindingSource
Dim WithEvents dgv As New DataGridView With _
{.Parent = Me, .Width = 600, .Height = 400, .Top = 40, _
.DataSource = bs, .AllowUserToAddRows = False}
Dim dt As New DataTable
Dim gc As New Subro.Controls.DataGridViewGrouperControl _
With {.Parent = Me}
Dim WithEvents grp As Subro.Controls.DataGridViewGrouper
Private Sub frmDatagridviewGrouper_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.Size = New Size(620, 480)
'Test-Spalten in Datatable erstellen
With dt.Columns
.Add("Groups")
.Add("Whatever1") : .Add("Whatever2")
.Add("Numbers", GetType(Integer))
.Add("Doubles", GetType(Double))
.Add("Dates", GetType(DateTime))
End With
'Test-Daten in Datatable erzeugen
Dim rndm As New Random
For i As Integer = 0 To 100
Dim r As DataRow = dt.NewRow
r("Groups") = "Group_" & rndm.Next(1, 6).ToString
r("Whatever1") = "WE1 _ " & rndm.Next(10, 20).ToString
If i Mod 3 = 0 Then
r("Whatever2") = DBNull.Value
Else
r("Whatever2") = "WE2 _ " & rndm.Next(100, 120).ToString
End If
If i Mod 5 = 0 Then
r("numbers") = DBNull.Value 'Integer.MinValue 'DBNull.Value
Else
r("numbers") = rndm.Next(-10, 11)
End If
If i Mod 4 = 0 Then
r("doubles") = DBNull.Value
ElseIf i Mod 7 = 0 Then
r("Doubles") = Double.NaN
Else
r("doubles") = Math.Round(rndm.NextDouble(), 1)
End If
r("Dates") = _
New DateTime(CLng((DateTime.MaxValue.Ticks * rndm.NextDouble)))
If i Mod 7 = 0 Then
r("Dates") = DBNull.Value
End If
dt.Rows.Add(r)
Next i
bs.DataSource = dt 'Datenbindung herstellen
'Gruppierung ermöglichen
grp = New Subro.Controls.DataGridViewGrouper(dgv)
gc.Grouper = grp 'Gruppierungs-Control zuordnen
With (grp)
.SetGroupOn("Groups") 'Gruppen-Spalte einrichten
'Beispiele/Erläuterungen von Codeproject (Robert Verpalen)
With .Options
'to start with all rows collapsed on a (re)load or
'when the group is changed you can set the option
' startcollapsed:
.StartCollapsed = True
'if you don't want the grouped column name
'to be repeated in the headers:
.ShowGroupName = True
'if you don't want the (rowcount) to be shown in the headers:
.ShowCount = False
.GroupSortOrder = SortOrder.Descending
End With
'to collapse all loaded rows:
'(the difference with setting the option above,
'is that after choosing a new grouping (or on a reload),
'the new groups would expand.
.CollapseAll()
End With
End Sub
Private Sub grp_DisplayGroup(sender As Object, _
e As Subro.Controls.GroupDisplayEventArgs) Handles grp.DisplayGroup
With e
If .Selected Then
.BackColor = Color.Blue
.ForeColor = Color.White
Else
.BackColor = _
If(.Group.GroupIndex Mod 2 = 0, Color.Orange, Color.LightBlue)
.ForeColor = Color.Black
End If
.Font = New Font("Arial", 12, FontStyle.Bold)
.Header = "[" & .Header & "], grp: " & .Group.GroupIndex.ToString
If .DisplayValue.Trim = String.Empty Then _
.DisplayValue = " <fehlend> "
If .DisplayValue.Length > 15 Then _
.DisplayValue = .DisplayValue.Substring(0, 15) & "..."
.DisplayValue = "value is " & .DisplayValue
.Summary = "contains " + .Group.Count.ToString & " rows"
End With
End Sub
Private Sub dgv_DataError(sender As Object, _
e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
Handles dgv.DataError
If e.ColumnIndex < 0 Then
e.Cancel = True : Exit Sub
End If
End Sub
End Class |