Summary Total Always Zero Values on ChildTemplate

1 Answer 82 Views
GridView
Agung
Top achievements
Rank 1
Agung asked on 01 May 2022, 03:52 AM

Hi, im using Hierarchy data that got stuck on show totals with gridviewgroup but the values always zero on childtemplate

i'm attached files with no group and it works fine. otherwise still zero if using group on childtemplate.

        private void SetDataGridTemplate()
        {
            this.datagrid.Columns.Clear();
            this.datagrid.GroupDescriptors.Clear();
            this.datagrid.Templates.Clear();
            this.datagrid.Relations.Clear();

            this.datagrid.Columns.AddRange(
                new GridViewDecimalColumn("idsub"),
                new GridViewTextBoxColumn("name"),
                    new GridViewTextBoxColumn("unit"),
                new GridViewDecimalColumn("price"));

            var child = new GridViewTemplate();
            child.Columns.AddRange(
                new GridViewDecimalColumn("idsub"),
                new GridViewTextBoxColumn("itemname"),
                new GridViewDecimalColumn("qty"),
                new GridViewTextBoxColumn("satuan"),
                new GridViewDecimalColumn("price"),
                new GridViewDecimalColumn("total"),
                new GridViewDecimalColumn("idcategory"),
                new GridViewTextBoxColumn("category")
                );
            this.datagrid.Templates.Add(child);
            var grupKategori = new GroupDescriptor
            {
                Format = "{0}{1}"
            };
            grupKategori.GroupNames.Add("idcategory", ListSortDirection.Ascending);
            grupKategori.GroupNames.Add("category", ListSortDirection.Ascending);
            this.datagrid.Templates[0].GroupDescriptors.Add(grupKategori);

            var item1 = new GridViewSummaryItem { Name = "price", Aggregate = GridAggregateFunction.Sum, FormatString = "" };
            var item2 = new GridViewSummaryItem { Name = "total", Aggregate = GridAggregateFunction.Sum, FormatString = "{0:0}" };
            var rowSummary = new GridViewSummaryRowItem(new[] { item1, item2 });
            this.datagrid.Templates[0].SummaryRowsBottom.Add(rowSummary);

            this.datagrid.Templates[0].ShowParentGroupSummaries = true;
            this.datagrid.Templates[0].ShowTotals = true;

            var relation = new GridViewRelation();
            relation.ParentTemplate = this.datagrid.MasterTemplate;
            relation.ChildTemplate = this.datagrid.Templates[0];
            relation.ParentColumnNames.Add("idsub");
            relation.ChildColumnNames.Add("idsub");
            this.datagrid.Relations.Add(relation);
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            this.SetDataGridTemplate();
            this.datagrid.DataSource = GetParentSource();
            this.datagrid.Templates[0].DataSource = GetChildSource();
        }

        private void datagrid_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            switch (e.SummaryItem.Name)
            {
                case "idcategory":
                    string[] arr = e.Value.ToString().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    string strvalue = e.Value.ToString();
                    if (strvalue.Contains(","))
                    {
                        arr = e.Value.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (arr.Length > 1)
                        {
                            e.FormatString = arr[1];
                        }
                    }
                    break;
                case "total":
                    switch (e.Parent)
                    {
                        case GridViewGroupRowInfo _:
                            e.FormatString = "{0:0}";
                            break;
                        case GridViewHierarchyRowInfo _:
                            e.FormatString = "{0:0}";
                            break;
                    }
                    break;
                case "price":
                    if (e.Parent is GridViewGroupRowInfo GroupRow)
                    {
                        e.FormatString = $"SubTotal {GroupRow.HeaderText}";
                    }
                    else if (e.Parent == this.datagrid.MasterView.ViewTemplate)
                    {
                        e.FormatString = "Total ";
                    }
                    else if (e.Parent is GridViewHierarchyRowInfo h)
                    {
                        e.FormatString = "Total ";
                    }
                    break;
            }
        }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 May 2022, 08:51 AM
Hello, Agung,   

Following the provided code snippet I created a sample project to test the behavior on my end. Indeed, after grouping by the child columns, the summary value is not calculated correctly. It seems that you hit a known issue.

I have logged it in our feedback portal by creating a public thread on your behalf. You can track its progress, subscribe for status changes and add your comments on the following link - feedback item.

I have also updated your Telerik points.

Currently, the possible solution that I can suggest is to create a custom GridViewSummaryItem and override its Evaluate method. Thus, you can write your own logic for summary item evaluation. 

    var item2 = new CustomSummaryItem("total","", GridAggregateFunction.Sum);
        public class CustomSummaryItem : GridViewSummaryItem
        {
            public CustomSummaryItem(string name, string formatString, GridAggregateFunction aggregate) : base(name, formatString, aggregate)
            {
            }
            public override object Evaluate(IHierarchicalRow row)
            {
                decimal total = 0;
                foreach (GridViewRowInfo childRow in row.ChildRows)
                {
                    GridViewGroupRowInfo groupRow = childRow as GridViewGroupRowInfo;
                    GridViewDataRowInfo dataRow = childRow as GridViewDataRowInfo;

                    if (groupRow != null)
                    {
                        foreach (GridViewRowInfo row1 in groupRow.ChildRows)
                            total += (decimal)(row1.Cells["total"].Value);
                    }
                    else if (dataRow != null)
                        total += (decimal)dataRow.Cells["total"].Value;
                }
                return total;
            }
        }

I have attached my sample project for your reference. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Agung
Top achievements
Rank 1
commented on 06 May 2022, 01:16 AM

Hi. Dess, many thanks for given code and it works.
Tags
GridView
Asked by
Agung
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or