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 display the Numberformat info in GridSummaryColumn of WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid
Tags: summaries

The group will be displayed in CaptionSummaryRow like Group Key, Group Value, ItemsCount while grouping a column in WPF DataGrid (SfDataGrid). Click here to know more about Grouping.

You can customize the summary text of CaptionSummaryRow by overriding GridCaptionSummaryCellRenderer class. You can set number format for summary text of GridCaptionSummaryRow using NumberFormatInfo like below code example.

C#

public class CustomCaptionSummaryCellRenderer : GridCaptionSummaryCellRenderer
{
    // Method to initialize the CaptionSummaryCell.
    public override void OnInitializeEditElement(DataColumnBase dataColumn, GridCaptionSummaryCell uiElement, object dataContext)
    {
        GridColumn column = dataColumn.GridColumn;
        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,
                    column.MappingName, this.DataGrid.View);
        }
    }
 
    // Method to update the CaptionSummaryCell
    public override void OnUpdateEditBinding(DataColumnBase dataColumn, GridCaptionSummaryCell element, object dataContext)
    {
        GridColumn column = dataColumn.GridColumn;
 
        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,
                    column.MappingName, this.DataGrid.View);
        }
    }
 
    // Method to get the Grouped Column.
    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;
            }
        }
        return null;
    }     
 
    // Method to Customize the CaptionSummaryCell Text.
    private string GetCustomizedCaptionText(string columnName, object groupName, int itemsCount)
    {
        //entryText - instead of "Items", the entryText is assigned to Customize the CaptionSummaryCell Text.
        groupName = GetNumberFormatInfo(groupName,columnName);
 
        string entryText = "Items";
        return string.Format("{0}: {1} - {2} {3}", columnName, groupName, itemsCount, entryText);
    }
 
    private string GetNumberFormatInfo(object value, string columnName)
    {
        if (columnName == "EmployeeSalary")
        {
            //based on the column name, you can set the NumberFormatInfo to the summary columns.
            var cultureName = CultureInfo.CurrentCulture.Name;
            NumberFormatInfo nfi = new CultureInfo(cultureName, false).NumberFormat;
            double formatedNumber = (double)value;
            nfi.NumberDecimalDigits = 4;
            int[] mySizes = { 2, 3, 0 };
            nfi.NumberGroupSizes = mySizes;
            nfi.NumberGroupSeparator = "*";
            nfi.NumberDecimalSeparator = "..";
 
            var content = formatedNumber.ToString("N", nfi);
            return content;
        }
        return value.ToString();
    }
}

Refer to the following code example to replace the default GridCaptionSummaryCellRenderer with customized CustomCaptionSummaryCellRenderer in SfDataGrid.CellRenderer collection.

C#

protected override void OnAttached()
{
    //default CaptionSummaryCellRenderer is removed.            
    this.AssociatedObject.CellRenderers.Remove("CaptionSummary");
 
    //Customized CaptionSummaryCellRenderer is added.
    this.AssociatedObject.CellRenderers.Add("CaptionSummary", new CustomCaptionSummaryCellRenderer());
}

View sample in GitHub.

2X faster development

The ultimate WPF 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