This is a migrated thread and some comments may be shown as answers.

RadGrid sum expression for child data

3 Answers 271 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yash
Top achievements
Rank 1
Yash asked on 12 Dec 2011, 01:18 PM
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

3 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 15 Dec 2011, 02:02 PM
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.

0
Martin
Top achievements
Rank 1
answered on 13 Dec 2013, 10:11 PM
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?
0
George
Telerik team
answered on 18 Dec 2013, 12:21 PM
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 >>
Tags
GridView
Asked by
Yash
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Martin
Top achievements
Rank 1
George
Telerik team
Share this question
or