RadGrid sum expression for child data

4 posts, 0 answers
  1. Yash
    Yash avatar
    3 posts
    Member since:
    Oct 2011

    Posted 12 Dec 2011 Link to this post

    Hello everyone,

    Here is my situation:

    I have a dataset with a relationship between two tables. These two tables are load in an hierarchical way in the radgridview.

    Now, in the child grid, I have a price column for each row.

    I am trying to create a calculated field in the master grid view which will reflect the total of the prices in the child grid. The code I am using is like the following:

    ...
    GridViewTemplate child = rdGvView.MasterTemplate.Templates[0];
    rdGvView.MasterTemplate.Columns["TotalPrice"].Expression = "Sum(child.prodPrice)";
    ....

    But this gives me an error which says {"Field name: child does not exist in the template."}

    I am using the sum expression from this link: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(VS.80).aspx

    where prodPrice is the column name of the table in the dataset which is imported in the radgrid.

    Thanks,
    Yash
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 15 Dec 2011 Link to this post

    Hello Yash,

    In order to support this functionality, you must use the Evaluate method of RadGridView control. Here is a simple example:

    using System;
    using System.Data;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace Lab
    {
        public partial class GridForm9 : Form
        {
            private DataTable rootTable;
            private DataTable childTable;
     
            public GridForm9()
            {
                InitializeComponent();
     
                radGridView1.CellValueChanged += new GridViewCellEventHandler(radGridView1_CellValueChanged);
            }
     
            void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
            {
                if (e.Column.OwnerTemplate != radGridView1.MasterTemplate && e.ColumnIndex == 3)
                {
                    EvaluateTotal((GridViewRowInfo)e.Row.Parent);
                }
            }
     
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
     
                BuildData();
                BuildHierarchy();
            }
     
            private void BuildHierarchy()
            {
                radGridView1.DataSource = rootTable;
     
                GridViewTemplate template = new GridViewTemplate();
                template.DataSource = childTable;
                radGridView1.Templates.Add(template);
     
                GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate, template);
                relation.ParentColumnNames.Add("Id");
                relation.ChildColumnNames.Add("ParentId");
                radGridView1.Relations.Add(relation);
     
                radGridView1.Columns.Add("Total");
                foreach (var item in radGridView1.ChildRows)
                {
                    EvaluateTotal(item);
                }
            }
     
            private void EvaluateTotal(GridViewRowInfo parent)
            {
                parent.Cells["Total"].Value = radGridView1.Evaluate("Sum(Count)", parent.ChildRows);
            }
     
            private void BuildData()
            {
                rootTable = new DataTable();
                rootTable.Columns.Add("Id", typeof(int));
                rootTable.Columns.Add("Name");
              
                rootTable.Rows.Add(0, "Ivan Petrov");
                rootTable.Rows.Add(1, "Jack Seven");
     
                childTable = new DataTable();
                childTable.Columns.Add("Id", typeof(int));
                childTable.Columns.Add("ParentId", typeof(int));
                childTable.Columns.Add("Name");
                childTable.Columns.Add("Count");
                childTable.Rows.Add(0, 0, "Monitors", 2);
                childTable.Rows.Add(1, 0, "Keyboards", 3);
                childTable.Rows.Add(2, 1, "Monitors", 4);
                childTable.Rows.Add(3, 1, "Keyboards", 5);
     
            }
        }
    }

    I hope this helps.

    Regards,
    Julian Benkov
    the Telerik team

    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

  3. Martin
    Martin avatar
    3 posts
    Member since:
    Dec 2013

    Posted 13 Dec 2013 Link to this post

    Looks like this sample is off by one?

    If I hook the CellValueChanged event on the grid and then check the ChildRows property it is always 1 less than the actual count in the child grid. Is that because the row has actually not been added yet to the ChildRows collection but will be done after the CellChanged event?
  4. George
    Admin
    George avatar
    500 posts

    Posted 18 Dec 2013 Link to this post

    Hi Martin,

    Thank you for writing.

    I created a sample project in which I tried to reproduce the behavior you are describing. As you can see in my application the ChildRows collection has a correct count. Below you can find the above mentioned project. Feel free to modify it in a way that the behavior is reproduced or provide me with another project of your own. Also feel free to share some additional information that may be of help.

    Looking forward to your response.

    Regards,
    George
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top