# Custom Aggregate Function Problem

Grid
Top achievements
Rank 1
Anon asked on 22 Jul 2010, 08:00 PM
I'm having a problem calculating my custom aggregate sum function for nested groups.

For example, I have a product group, a product, and then several rows pertaining to that product (so there are two groupings: the main product group > then the individual product and its data). Then, for each grouping I have a footer which calculates the sum of the column based on the value of another column in that row.

What I'd like to see is something like this assuming a product group with 2 products. It would calculate the footer total for each product in that column if the first column's value is say 'Row 3' or 'Row 4' and then for the top product grouping (for 'Product Group 1') would calculate the values from the other footers.

Product Group 1
Product 1
Row 1 | 1 | 1 | 1 | 1 |
Row 2 | 1 | 1 | 1 | 1 |
Row 3 | 2 | 2 | 2 | 1 |
Row 4 | 2 | 2 | 1 | 1 |
[Footer]   | 4 | 4 | 3 | 2 |
Product 2
Row 1 | 1 | 1 | 1 | 1 |
Row 2 | 1 | 1 | 1 | 1 |
Row 3 | 2 | 2 | 2 | 1 |
Row 4 | 2 | 2 | 1 | 1 |
[Footer]   | 4 | 4 | 3 | 2 |
[Footer]      | 8 | 8 | 6 | 4 |

I have it set up so each product's footer calculates the individual product's footer correctly, but not the top most group footer (the product grouping), it only calculates the last individual product's total as in the screenshot I attached. I'm not quite sure how to grab the totals from previous footers of the same grouping.

Here is my custom aggregate function as is now:
`Protected` `Sub` `RadGrid1_CustomAggregate(``ByVal` `sender ``As` `Object``, ``ByVal` `e ``As` `GridCustomAggregateEventArgs) ``Handles` `RadGrid1.CustomAggregate`

`        ``If` `e.Column.UniqueName = ``"Row"` `Then`
`            ``e.Result = ``""`
`        ``Else`
`            ``Dim` `length ``As` `Integer` `= RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader).Length`
`            ``For` `i ``As` `Integer` `= 0 ``To` `length - 1`

`                ``Dim` `total ``As` `Double` `= 0`

`                ``Dim` `groupHeader ``As` `GridGroupHeaderItem = ``DirectCast``(RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)(i), GridGroupHeaderItem)`
`                ``Dim` `children ``As` `GridItem() = groupHeader.GetChildItems()`

`                ``For` `Each` `child ``As` `GridItem ``In` `children`
`                    ``If` `TypeOf` `child ``Is` `GridDataItem ``Then`
`                        ``Dim` `childItem ``As` `GridDataItem = TryCast(child, GridDataItem)`

`                        ``Dim` `rowText ``As` `String` `= childItem.Item(``"Row"``).Text`

`                        ``If` `rowText = ``"Row3"` `OrElse` `rowText = ``"Row4"` `Then`
`                            ``total += childItem.Item(``"Value"``).Text`
`                        ``End` `If`
`                    ``End` `If`
`                ``Next`
`                ``e.Result = total`
`            ``Next`
`        ``End` `If`
`    ``End` `Sub`

Sorry if this is a dumb question, but I'm a little stuck here.

## 1 Answer, 1 is accepted

0
Martin
Telerik team
answered on 28 Jul 2010, 11:58 AM
Hello Anon,

You can use the GroupIndex property of the GridGroupFooterItem and GridGroupHeaderItem objects to distinguish which footer item belongs to the relevant group and calculate its aggregate accordingly. I have attached a small sample that demonstrates this approach.

I hope this helps.

Regards,
Martin
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
Tags
Grid