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.

//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);
       }
    }
}
'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

Screenshots:

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

 

Conclusion


I hope you enjoyed learning how to iterate through groups in a WinForms GridGroupingControl.


You can refer to our WinForms GridGroupingControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to present and manipulate data.

 

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms GridGroupingControl and other WinForms components.

 

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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