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

Summary by group

Hi,

I'm having trouble with summaries on a Datagrid.
I have a datagrid with 2 levels of grouping and would like to have totals on each level.
looking something like:

Country     Category   Shop         Amount
Belgium                                       100
Belgium     Pets                           75
Belgium     Pets          Petstore    50
Belgium     Pets          Petland     25
Belgium     Toys                          25
Belgium     Toys        Toysrus     25

I've tried this using a CaptionSummaryRow, but this always seems to delete the grouping column value.

Any help would be welcome
Thanks

1 Reply

SP Shobika Palani Syncfusion Team January 2, 2019 04:17 PM UTC

Hi Ivo, 

Thank you for contacting Syncfusion Support. 

We have analyzed your query to show summary on each level of groups. You can achieve this by customizing the GridCaptionSummaryCellRenderer as like the following code snippet 

public class CustomCaptionSummaryCellRenderer : GridCaptionSummaryCellRenderer 
    { 
        public override void OnInitializeEditElement(DataColumnBase dataColumn, GridCaptionSummaryCell uiElement, object dataContext) 
        { 
            base.OnInitializeEditElement(dataColumn, uiElement, dataContext); 
            if (dataContext is Group) 
            { 
                var groupRecord = dataContext as Group; 
                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); 
                    if (dataColumn.GridColumn == this.DataGrid.Columns.FirstOrDefault(col => !col.IsHidden)) 
                    { 
                        uiElement.Content = groupRecord.Key.ToString() +" - " + uiElement.Content.ToString(); 
                    } 
                } 
            } 
        } 
 
        public override void OnUpdateEditBinding(DataColumnBase dataColumn, GridCaptionSummaryCell element, object dataContext) 
        { 
            base.OnUpdateEditBinding(dataColumn, element, 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.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); 
 
                    if (dataColumn.GridColumn == this.DataGrid.Columns.FirstOrDefault(col => !col.IsHidden)) 
                    { 
                        element.Content = groupRecord.Key.ToString() +" - "+ element.Content.ToString(); 
                    } 
                } 
            } 
        } 
         
        // 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; 
        } 
    } 

Please find sample for the same from the following link 
Sample Link: 

Please let us know your concern. 

Regards, 
Shobika. 


Loader.
Live Chat Icon For mobile
Up arrow icon