Articles in this section
Category / Section

How to customize the CaptionSummaryCell text in WPF DataGrid?

2 mins read



In the WPF DataGrid, GridCaptionSummaryCell displays the GroupCaption and the Caption Summary value. You can refer here to know more about the Caption Summaries. By default, the GroupCaption text is displayed in the {ColumnName}: {Key} – {ItemsCount} Items format. You can customize the GroupCaptionText with the help of the GroupCaptionTextFormat. You can refer here to know more about the GroupCaptionTextFormat.

By default, the CaptionSummaryCell text is displayed as follows.

C:\Users\arshiazeba\Desktop\image1.png

Figure 1: CaptionSummaryCell text – Default format

By using the GroupCaptionTextFormat, you cannot modify the GroupName, Group KEY, and itemscount in different formats for the available groups based on some conditions. To achieve this, you need to derive a new class from the GridCaptionSummaryCellRenderer and override the OnInitializeEditElement () and OnUpdateEditBinding () methods in the derived CustomCaptionSummarycellRenderer. You can refer to the following code example to derive a new class from the GridCaptionSummaryCellRenderer.

C#

public class CustomCaptionSummaryCellRenderer : GridCaptionSummaryCellRenderer
{
….
}

Group KEY and items (Default Text) in the CaptionSummaryCell text are displayed in different formats based on the itemscount. For example, in the code example, Group KEY 1000 is changed into one thousand based on the group name and the default text, items, is changed into entries in the group or elements in the group based on the itemscount.

OnInitializeEditElement method is used to initialize the content of the CaptionSummaryCell by using the grouped column details and it is invoked while creating a SpannedDataColumn in the CaptionSummaryRow.

You can refer to the following code example to override the OnInitializeEditElement ().

C#

/// <summary>
/// Method to initialize the CaptionSummaryCell.
/// </summary>
public override void OnInitializeEditElement(DataColumnBase dataColumn, GridCaptionSummaryCell uiElement, object dataContext)
{
    if (dataContext is Group)
    {
        var groupRecord = dataContext as Group;
        if (this.DataGrid.CaptionSummaryRow == null)
        {
            // get the column which is grouped
            var groupedColumn = this.GetGroupedColumn(groupRecord);
            //set the captionsummarycell text as customized.
            uiElement.Content = GetCustomizedCaptionText(groupedColumn.HeaderText, groupRecord.Key,
                groupRecord.ItemsCount);
        }
        else if (this.DataGrid.CaptionSummaryRow.ShowSummaryInRow)
        {
            uiElement.Content = SummaryCreator.GetSummaryDisplayTextForRow(groupRecord.SummaryDetails,
                this.DataGrid.View);
        }
        else
            uiElement.Content = SummaryCreator.GetSummaryDisplayText(groupRecord.SummaryDetails,
                dataColumn.GridColumn.MappingName, this.DataGrid.View);
    }
}

In the OnInitializeEditElement method, CaptionSummaryCell text can be customized by using grouped column name, Group KEY and the itemscount. By using GetGroupedColumn method, the grouped column is obtained. ShowSummaryInRow property in GridSummaryRow is used to decide whether the summary value is displayed in the specific row or column. By using GetDisplayTextForRow method and GetDisplayText method, CaptionSummaryCell text is displayed based on the ShowSummaryInRow property. You can refer here to know more about the GridSummaryRow properties.

OnUpdateEditBinding method is used to update the CaptionSummaryCell text when there is a change in grouping like itemscount, Group KEY, and grouped column name; and it is invoked when the CaptionSummaryRow is updated and also whenever the grid is refreshed. You can refer to the following code example to override the OnUpdateEditBinding ().

C#

/// <summary>
/// Method to update the CaptionSummaryCell.
/// </summary>
public override void OnUpdateEditBinding(DataColumnBase dataColumn, GridCaptionSummaryCell element, object dataContext)
{
    if (element.DataContext is Group && this.DataGrid.View.GroupDescriptions.Count > 0)
    {
        var groupRecord = element.DataContext as Group;
        //get the column which is grouped.
        var groupedColumn = this.GetGroupedColumn(groupRecord);
        if (this.DataGrid.CaptionSummaryRow == null)
        {
            if (this.DataGrid.View.GroupDescriptions.Count < groupRecord.Level)
                return;
            //set the captionsummary text as customized.
            element.Content = GetCustomizedCaptionText(groupedColumn.HeaderText, groupRecord.Key,
                groupRecord.ItemsCount);
        }
        else if (this.DataGrid.CaptionSummaryRow.ShowSummaryInRow)
        {
            element.Content = SummaryCreator.GetSummaryDisplayTextForRow(groupRecord.SummaryDetails,
                this.DataGrid.View, groupedColumn.HeaderText);
        }
        else
            element.Content = SummaryCreator.GetSummaryDisplayText(groupRecord.SummaryDetails,
                dataColumn.GridColumn.MappingName, this.DataGrid.View);
    }
}

You can refer to the following code example for GetGroupedColumn ().

C#

/// <summary>
/// Method to get the Column that is grouped.
/// </summary>
private GridColumn GetGroupedColumn(Group group)
{
    var groupDesc = this.DataGrid.View.GroupDescriptions[group.Level - 1] as PropertyGroupDescription;
    foreach (var column in this.DataGrid.Columns)
    {
        if (column.MappingName == groupDesc.PropertyName)
        {
            return column;
        }
    }2
    return null;
}

You can refer to the following code example for GetCustomizedCaptionText ().

C#

/// <summary>
/// Method to Customize the CaptionSummaryCell Text.
/// </summary>
private string GetCustomizedCaptionText(string columnName, object groupName, int itemsCount)
{
            //entryText - instead of "Items", the entryText is assigned to Customize the CaptionSummaryCell Text.
            string entryText = string.Empty;
            if (itemsCount < 20)
                entryText = "entries in the Group";
            else if (itemsCount < 40)
                entryText = "elements in the Group";
            else if (itemsCount < 60)
                entryText = "list in the Group";
            else
                entryText = "items in the Group";
            if (groupName.ToString().Equals("1000"))
                groupName = "One Thousand";
            else if (groupName.ToString().Equals("1002"))
                groupName = "Thousand and Two";
            else if (groupName.ToString().Equals("1004"))
                groupName = "Thousand and Four";
           return string.Format("{0}: {1} - {2} {3}", columnName, groupName, itemsCount, entryText);
}

In the GetCustomizedCaptionText method, the CaptionSummaryCell text is framed and customized text is returned. You can refer to the following code example to remove the default GridCaptionSummaryCellRenderer and add the derived GridCaptionSummaryCellRenderer to the renderers’ collection in the SfDataGrid.

C#

//Default CaptionSummaryCellRenderer CellRenderer is removed.
this.datagrid.CellRenderers.Remove("CaptionSummary");
//Customized CaptionSummaryCellRenderer is added.
this.datagrid.CellRenderers.Add("CaptionSummary", new CustomCaptionSummaryCellRenderer());

After the customization of the CaptionSummaryCell text, the CaptionSummaryCell is displayed as follows.

     Figure 2: Customized CaptionSummaryCell text

In the above screenshot, the Group KEY is changed as One Thousand instead of 1000, thousand and two instead of 1002 and also the default text, items, is changed as entries in the group, elements in the group based on the itemscount.

You can refer to the sample from following location.

Samples

WPF

WinRT

Silverlight


Conclusion

I hope you enjoyed learning about how to customize the CaptionSummaryCell text in WPF DataGrid.


You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our WPF DataGrid example
to understand how to create and manipulate data.

For current customers, you can check out our 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 other controls.

If you have any queries or require clarifications, please let us know in the comments section 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