Articles in this section
Category / Section

How to iterate through groups in a WinForms GridGroupingControl?

2 mins read

Iterate groups

Under a group, there may be either records or sub-groups. A group has two properties, group.Groups and group.Records. When the parent group contains records, then group.Records can be accessed, and when the parent group contains child groups, you can iterate through the group.Groups collection. This can be achieved using a simple recursive function.

C#

//Get the groups for iteration.
Group group = this.gridGroupingControl1.Table.TopLevelGroup;
// Invoke Iterate method
this.Iterate(group);
…
public void Iterate(Group g)
{
    int indentLength = 4 * (g.GroupLevel + 1);
    string indent = new string(' ', indentLength);
    Console.WriteLine(indent + "GroupLevel = " + g.GroupLevel);
    Console.WriteLine(indent + g.Info);
    indent = new string(' ', indentLength + 2);
    //Check the group contains Records.
    if(g.Records != null && g.Records.Count > 0)
    {
       foreach (Record r in g.Records)
       {
          Console.WriteLine(indent + r.Info);
       }
    }
    //Check the Group has sub groups.
    else if (g.Groups != null && g.Groups.Count > 0)
    {
       foreach (Group gr in g.Groups)
       {
          //Recursive call for the Iterate with sub group.
          Iterate(gr);
       }
    }
}

VB

'Get the groups for iteration.
Dim group As Group = Me.gridGroupingControl1.Table.TopLevelGroup
'Invoke Iterate method.
Me.Iterate(group)
…
Public Sub Iterate(ByVal g As Group)
    Dim indentLength As Integer = 4 * (g.GroupLevel + 1)
    Dim indent As New String(" "c, indentLength)
    Console.WriteLine(indent & "GroupLevel = " & g.GroupLevel)
    Console.WriteLine(indent & g.Info)
    indent = New String(" "c, indentLength + 2)
    'Check the group contains Records.
    If g.Records IsNot Nothing AndAlso g.Records.Count > 0 Then
       For Each r As Record In g.Records
           Console.WriteLine(indent & r.Info)
       Next r
    'Check the Group has sub groups.
    ElseIf g.Groups IsNot Nothing AndAlso g.Groups.Count > 0 Then
       For Each gr As Group In g.Groups
           'Recursive call for the Iterate  with sub group.
           Iterate(gr)
       Next gr
    End If
End Sub

Screenshot

Show the iterate groups in GridGroupingControl

Figure 1: GridGroupingControl

Show the output of iteration

Figure 2: Screenshot of the output

Samples:

C#: IterateGroups

VB: IterateGroups

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied