How to customize the group caption text in WinForms DataGrid?
Customize the group caption text
WinForms DataGrid, 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

Samples:
C#: GroupCaptionText
VB: GroupCaptionText
Conclusion
I hope you enjoyed learning about how to customize the group caption text in WinForms DataGrid (SfDataGrid).
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms DataGrid 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 DataGrid 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!