We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize the group caption text in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid |
Published Date: September 27, 2018 |
Last Revised Date: January 12, 2020

Customize the group caption text

By default, the group caption text contains the mapping name of the grouped column, key value, and number of items in the group. This caption text can be changed by using the SfDataGrid.DrawCell event.

In the example, the group caption text is customized to display the total number of records and subgroups in the group, if any.

C#

sfDataGrid.DrawCell += sfDataGrid_DrawCell;
 
void sfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e)
{
    if (e.DataRow.RowType == RowType.CaptionCoveredRow && !string.IsNullOrEmpty(e.DisplayText))
    {
        var displayText = string.Empty;
        var group = (e.DataRow.RowData as Syncfusion.Data.Group);
        if (group != null)
        {
            if (group.Groups != null)
            {
                int records = 0;
                records = GetTotalRecordsCount(group);
                displayText = group.Key.ToString() + ": " + records + " Records (" + group.Groups.Count + " Sub Groups)";
            }
            else if (group.Records != null)
                displayText = group.Key.ToString() + ": " + group.Records.Count + " Records";
            e.DisplayText = displayText;
        }
    }
}
 
private int GetTotalRecordsCount(Group group)
{
    int count = 0;
 
    if (group.Groups != null)
    {
        foreach (var g in group.Groups)
        {
            if (g.Groups != null)
                foreach (var g1 in g.Groups)
                    count += GetTotalRecordsCount(g1);
 
            if (g.Records != null)
                count += g.Records.Count;
        }
    }
    else
        if (group.Records != null)
            count += group.Records.Count;
 
    return count;
}

VB

AddHandler sfDataGrid.DrawCell, AddressOf sfDataGrid_DrawCell
 
Private Sub sfDataGrid_DrawCell(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs)
 If e.DataRow.RowType = RowType.CaptionCoveredRow AndAlso (Not String.IsNullOrEmpty(e.DisplayText)) Then
  Dim displayText = String.Empty
  Dim group = (TryCast(e.DataRow.RowData, Syncfusion.Data.Group))
  If group IsNot Nothing Then
   If group.Groups IsNot Nothing Then
    Dim records As Integer = 0
    records = GetTotalRecordsCount(group)
    displayText = group.Key.ToString() & ": " & records & " Records (" & group.Groups.Count & " Sub Groups)"
   ElseIf group.Records IsNot Nothing Then
    displayText = group.Key.ToString() & ": " & group.Records.Count & " Records"
   End If
   e.DisplayText = displayText
  End If
 End If
End Sub
 
Private Function GetTotalRecordsCount(ByVal group As Group) As Integer
 Dim count As Integer = 0
 
 If group.Groups IsNot Nothing Then
  For Each g In group.Groups
   If g.Groups IsNot Nothing Then
    For Each g1 In g.Groups
     count += GetTotalRecordsCount(g1)
    Next g1
   End If
 
   If g.Records IsNot Nothing Then
    count += g.Records.Count
   End If
  Next g
 Else
  If group.Records IsNot Nothing Then
   count += group.Records.Count
  End If
 End If
 
 Return count
End Function

 

Show the customized group caption text

Samples:

C#: GroupCaptionText

VB: GroupCaptionText

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile