This is a migrated thread and some comments may be shown as answers.
Calculated value from child records
4 Answers 43 Views
This is a migrated thread and some comments may be shown as answers.
Jose
Top achievements
Rank 2
Jose asked on 22 Mar 2011, 10:40 AM
Hi,

I would like to know if I can have a column in the master grid that it's a sum of child column?

For example, I have a master "invoices" grid with the "invoice lines" child grid. In the "invoices" grid I want a column with the total price of invoice, calculated from the sum of price column in "invoice lines" grid.

It's possible?
Regards
Jose

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 22 Mar 2011, 12:46 PM
Hello Jose,

You can access the nested table view (detail table) of the each item and traverse each detail table item to calculate the value(cell value) .Then assign it to corresponding column(cell) of parent item. The following documentation will help you to access the nested table view of each item.
Traversing detail tables/items in Telerik RadGrid
Also refer the following documentation for accessing cells and rows in grid item.
Accessing cells and rows

Thanks,
Princy.
0
Jose
Top achievements
Rank 2
answered on 22 Mar 2011, 04:49 PM
Hi Princy,

Thank you very much for your help.
I've tried with your suggestions but I'm unable to make it works.

This is the code that I'm trying in ItemDataBound event but it fails:

// Calculate Invoice Total column
if (e.Item.OwnerTableView.Name == "Master")
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        decimal totalInvoice = 0;
 
        GridTableView detailsTable = item.ChildItem.NestedTableViews[0];
 
        foreach (GridDataItem childItem in detailsTable.Items)
        {
            totalInvoice += System.Convert.ToDecimal(childItem["Price"].Text);
        }
 
        item["TotalInvoice"].Text = String.Format("{0:c}", totalInvoice);
    }
}

Any idea?
Regards
Jose
0
Accepted
Princy
Top achievements
Rank 1
answered on 23 Mar 2011, 07:06 AM
Hello Jose,

Try the following code snippet in PreRender event and check if it works now.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
       foreach (GridDataItem item in RadGrid1.Items)
        {
            if (item.Expanded)
            {
                GridTableView tableView = (GridTableView)item.ChildItem.NestedTableViews[0];
                decimal totalInvoice = 0;
                foreach (GridDataItem childitem in tableView.Items)
                {
                   totalInvoice += System.Convert.ToDecimal(childItem["Price"].Text);
                }
            }
        }
   }

Thanks,
Princy.
0
Jose
Top achievements
Rank 2
answered on 24 Mar 2011, 12:12 PM
Thank you very much Princy.
I've added to the grid HierarchyLoadMode="ServerBind" because I need the totals in every row and not only expanded rows.
Also I've changed "if (item.expanded)" by "if (item.ChildItem != null)".

Thank you very much for your help.
Regards
Jose
Tags
Grid
Asked by
Jose
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 1
Jose
Top achievements
Rank 2
Share this question
or