Add subtotal on every hierarchy

7 posts, 0 answers
  1. andi
    andi avatar
    18 posts
    Member since:
    Oct 2018

    Posted 05 Nov 2018 Link to this post

    how to add subtotal in every hierarchy, then on 0 level hierarchy or parent we have the summary from child rows under each parent, i assume we use 2 level hierarchy start from 0. thanks for helping.
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3575 posts

    Posted 06 Nov 2018 Link to this post

    Hello, Andi, 
      
    In order to add a summary row at the master level that aggregates data from the child template, you can create a custom GridViewSummaryItem and override its Evaluate method where you can control how exactly the calculation is executed. In the below sample code snippet it is demonstrated how to iterate the child rows and sum the UnitPrice field for each product: 

    private void RadForm1_Load(object sender, EventArgs e)
    {
        this.productsTableAdapter.Fill(this.nwindDataSet.Products);
        this.categoriesTableAdapter.Fill(this.nwindDataSet.Categories);
        radGridView1.AutoGenerateHierarchy = true;
        radGridView1.DataSource = this.nwindDataSet;
        radGridView1.DataMember = "Categories";
     
        this.radGridView1.MasterTemplate.BestFitColumns(Telerik.WinControls.UI.BestFitColumnMode.AllCells);
        this.radGridView1.MasterTemplate.Templates[0].BestFitColumns(Telerik.WinControls.UI.BestFitColumnMode.AllCells);
        CustomSummaryItem summaryItem = new CustomSummaryItem("Description", "Total price: {0}", GridAggregateFunction.Count);
        GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
        summaryRowItem.Add(summaryItem);
        this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
    }
     
    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 dataRow in row.ChildRows)
            {
                foreach (GridViewRowInfo childRow in dataRow.ChildRows)
                {
                    total += (decimal)childRow.Cells["UnitPrice"].Value;
                }
            }
            return total;
        }
    }



    You can refer to the following help article which last section also demonstrates a sample approach: https://docs.telerik.com/devtools/winforms/gridview/rows/summary-rows

    I hope this information helps. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. andi
    andi avatar
    18 posts
    Member since:
    Oct 2018

    Posted 06 Nov 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    thank for writing dess,

    then how to make subtotal for beverages and condiments before total price as the image attached.

     

  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3575 posts

    Posted 07 Nov 2018 Link to this post

    Hello, Andi, 

    If I understand your requirement correctly, you need to have the total for each master row calculating the value from all child rows associated with this parent row. You can add a summary item for the child template as well as it is demonstrated below: 

    GridViewSummaryItem summaryItem2 = new GridViewSummaryItem("UnitPrice", "Total price: {0}", GridAggregateFunction.Sum);
    GridViewSummaryRowItem summaryRowItem2 = new GridViewSummaryRowItem();
    summaryRowItem2.Add(summaryItem2);
    this.radGridView1.MasterTemplate.Templates[0].SummaryRowsTop.Add(summaryRowItem2);




    I hope this information helps.

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. andi
    andi avatar
    18 posts
    Member since:
    Oct 2018

    Posted 07 Nov 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hi dess,

    You understand it very well, i have implemented the codes but wrote in vb net, but still there is a problem sub total is always change when ever cell value in child rows changed, but grand total (total price at "Radgridview" mastertemplate) is not update the value until i click the cells.

        Private Sub RadForm4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Call KoneksiSQLServer()

            Cmd = New SqlCommand("SelectBarangKegiatanHLast", Conn)
            Cmd.CommandType = CommandType.StoredProcedure
            Dim daBarangKegiatanH As SqlDataAdapter = New SqlDataAdapter(Cmd)

            Cmd = New SqlCommand("SelectBarangKegiatanDLast", Conn)
            Cmd.CommandType = CommandType.StoredProcedure
            Dim daBarangKegiatanD As SqlDataAdapter = New SqlDataAdapter(Cmd)
            Dim dsBrgKeg As DataSet = New DataSet("BarangKegiatan")

            daBarangKegiatanH.FillSchema(dsBrgKeg, SchemaType.Source, "BarangKegiatanH")
            daBarangKegiatanH.Fill(dsBrgKeg, "BarangKegiatanH")
            daBarangKegiatanD.FillSchema(dsBrgKeg, SchemaType.Source, "BarangKegiatanD")
            daBarangKegiatanD.Fill(dsBrgKeg, "BarangKegiatanD")

            RadGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
            RadGridView1.DataSource = dsBrgKeg.Tables("BarangKegiatanH")

            template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
            template.DataSource = dsBrgKeg.Tables("BarangKegiatanD")

            RadGridView1.MasterTemplate.Templates.Add(template)

            Dim relation As New GridViewRelation(RadGridView1.MasterTemplate)
            relation.ChildTemplate = template
            relation.RelationName = "BarangKegiatanHH"
            relation.ParentColumnNames.Add("IdBrgKeg")
            relation.ChildColumnNames.Add("IdBrgKegD")
            RadGridView1.Relations.Add(relation)

            Dim summaryItem As CustomSummaryItem = New CustomSummaryItem("Satuan", "Total harga: {0}", GridAggregateFunction.Count)
            Dim summaryRowItem As GridViewSummaryRowItem = New GridViewSummaryRowItem()
            summaryRowItem.Add(summaryItem)
            Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)

            Dim summaryItem2 As GridViewSummaryItem = New GridViewSummaryItem("HargaD", "Sub Total Harga: {0}", GridAggregateFunction.Sum)
            Dim summaryRowItem2 As GridViewSummaryRowItem = New GridViewSummaryRowItem()
            summaryRowItem2.Add(summaryItem2)
            Me.RadGridView1.MasterTemplate.Templates(0).SummaryRowsTop.Add(summaryRowItem2)
        End Sub

        Public Class CustomSummaryItem
            Inherits GridViewSummaryItem

            Public Sub New(ByVal name As String, ByVal formatString As String, ByVal aggregate As GridAggregateFunction)
                MyBase.New(name, formatString, aggregate)
            End Sub

            Public Overrides Function Evaluate(ByVal row As IHierarchicalRow) As Object
                Dim total As Decimal = 0

                For Each dataRow As GridViewRowInfo In row.ChildRows

                    For Each childRow As GridViewRowInfo In dataRow.ChildRows
                        total += CDec(childRow.Cells("HargaD").Value)
                    Next
                Next

                Return total
            End Function
        End Class

  6. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3575 posts

    Posted 07 Nov 2018 Link to this post

    Hello, Andi, 

    In order to force the custom summary item from the master level to be refreshed properly after modifying a child value, you can use the InvalidateRow method for the summary row as it is demonstrated below. The attached gif file illustrates the achieved behavior: 

    this.radGridView1.CellValueChanged+=radGridView1_CellValueChanged;


    private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
    {
        if (e.Column.Name=="UnitPrice")
        {
            this.radGridView1.MasterView.SummaryRows[0].InvalidateRow();
        }
    }

    I hope this information helps.

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  7. andi
    andi avatar
    18 posts
    Member since:
    Oct 2018

    Posted 07 Nov 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    oke dess. thanks for your valueable reply.

    Regards

Back to Top