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

# GridCalculatedColumn ISSUE

4 Answers 143 Views
Grid
Top achievements
Rank 1
Mike asked on 16 Mar 2010, 10:10 PM
I have a customer that is looking for a custom aggregate in the footer using a GridCalculatedColumn.  As you can see from the attached image I am trying to take the following cells

(0.0732 * 61997914 / 111,335,022) = 0.0408
(0.0681 * 28971917 / 111,335,022) = 0.0177
(0.0719 * 20365191 / 111,335,022) = 0.0132
-----------
0.0716

How can I access the 111,335,022 if it is in the footer of a grouped column while performing this calculation at runtime and come up with the 0.0716 instead of the 0.0711 which is a straight average?

Is it possible to get these values in the ItemDataBound, if so I would have to distinguish them by the grouped columns.

## 4 Answers, 1 is accepted

0
Princy
Top achievements
Rank 2
answered on 17 Mar 2010, 08:39 AM
Hi,

Try the code snippet below to access the childitems and the group footer total to calculate the required amount. I have made use of a GridTemplateColumn instead of GridNumericColumn

ASPX:


C#
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item is GridGroupFooterItem) { GridGroupFooterItem groupFooterItem = (GridGroupFooterItem)e.Item; int totalValue2=Convert.ToInt32(groupFooterItem["Value2"].Text); foreach(GridDataItem dataItem in  groupFooterItem.OwnerTableView.Items) { Label lbl=(Label) dataItem["CalculatedTotal"].FindControl("lblTotal"); lbl.Text = Convert.ToString(Convert.ToDecimal(dataItem["Value1"].Text) * Convert.ToInt32(dataItem["Value2"].Text) / totalValue2); } } }

Hope this helps.

Princy
0
Mike
Top achievements
Rank 1
answered on 17 Mar 2010, 01:37 PM
Thanks for getting back to me, I like ths approach however it's throwing this error.

Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'Telerik.Web.UI.GridGroupFooterItem'

Source Error:

 ``` Line 252: { Line 253: GridGroupFooterItem groupFooterItem = (GridGroupFooterItem)e.Item; Line 254: int totalValue2 = Convert.ToInt32(groupFooterItem["total_kwh"].Text);Line 255: foreach (GridDataItem dataItem in groupFooterItem.OwnerTableView.Items) Line 256: { ```

0
Veli
Telerik team
answered on 19 Mar 2010, 01:43 PM
Hi Mike,

The indexed property of the GridGroupFooterItem object is added in the 2009.Q2 release of Telerik.Web.UI. If the compiler complains of no indexed property, you are probably using an older version of RadGrid?

Regards,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike
Top achievements
Rank 1
answered on 19 Mar 2010, 01:54 PM
Thanks for the update, looks like we need an upgrade.  Until then, I took some code from another post and made a custom aggregate which works great until we upgrade.  It's important to remember using the code below to make sure the columns are in order from left to right and use OnCustomAggregate="RadGrid1_CustomAggregate"

 protected void RadGrid1_CustomAggregate(object sender, GridCustomAggregateEventArgs e) { if (e.Item is GridGroupFooterItem && e.Column.UniqueName == "TotalkWh" || e.Item is GridGroupFooterItem && e.Column.UniqueName == "RunningPrice") { //  Response.Write("
Start
"); int totalkWh = 0; // Get the Total kWh for a grouping if (e.Item is GridGroupFooterItem && e.Column.UniqueName == "TotalkWh") { if (RadGrid1.MasterTableView.GroupByExpressions.Count != 0) { int gIndex = Convert.ToInt32(e.Item.GroupIndex) / 2; GridGroupHeaderItem hItem = (GridGroupHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[gIndex]; GridItem[] items = hItem.GetChildItems(); foreach (GridDataItem item in items) { if (!String.IsNullOrEmpty(Convert.ToString(item["TotalkWh"].Text))) { string stripCommas = item["TotalkWh"].Text; stripCommasstripCommas = stripCommas.Replace(",", ""); totalkWh = Convert.ToInt32(stripCommas) + totalkWh; } } e.Result = totalkWh.ToString("N0"); ViewState["totalkWh"] = totalkWh; //Response.Write("
Middle " + totalkWh + "
"); } } decimal avgPrice = 0; decimal avgPriceFinal = 0; if (e.Item is GridGroupFooterItem && e.Column.UniqueName == "RunningPrice") { //  Response.Write("
ViewState " + ViewState["totalkWh"] + "
"); if (RadGrid1.MasterTableView.GroupByExpressions.Count != 0) { int gIndex = Convert.ToInt32(e.Item.GroupIndex) / 2; GridGroupHeaderItem hItem = (GridGroupHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[gIndex]; GridItem[] items = hItem.GetChildItems(); avgPrice = 0; avgPriceFinal = 0; foreach (GridDataItem item in items) { if (!String.IsNullOrEmpty(Convert.ToString(item["RunningPrice"].Text))) { avgPrice = Convert.ToDecimal(Convert.ToString(item["RunningPrice"].Text)) / Convert.ToInt32(Convert.ToString(ViewState["totalkWh"])); avgPriceFinalavgPriceFinal = avgPriceFinal + avgPrice; } // Response.Write(Convert.ToString(item["RunningPrice"].Text) + " / " + Convert.ToString(ViewState["totalkWh"]) + " = " + avgPrice.ToString("N4") + "
"); } // / Convert.ToInt32(Convert.ToString(item["TotalkWh"].Text)) e.Result = avgPriceFinal.ToString("N4"); ViewState["avgPrice"] = avgPriceFinal.ToString("N4"); } } // Response.Write("
End "); } if (e.Item is GridGroupFooterItem && e.Column.UniqueName == "AvgPrice") { decimal  returnAvgPrice = Convert.ToDecimal(Convert.ToString(ViewState["avgPrice"])); e.Result = returnAvgPrice.ToString("C4"); } }

Tags
Grid
Mike
Top achievements
Rank 1