Custom Aggregate Multiple Groups

2 posts, 0 answers
  1. Bryan
    Bryan avatar
    9 posts
    Member since:
    May 2013

    Posted 16 Jan 2017 Link to this post



    I am looking to write a custom aggregate that sums a column but excludes certain rows.

    On this grid there are 2 groups applied. I have the sum working for the first level of groups but not the total of the second group level or the total of the whole grid

    They just seem to show the values of the row above


    Any ideas? 


     Private Sub grid_CustomAggregate(sender As Object, e As GridCustomAggregateEventArgs)
            Dim total As Decimal = 0
            if TypeOf (e.item) is GridGroupFooterItem then
                If TryCast(e.Column, GridBoundColumn).DataType = GetType(Decimal) Then
                    For Each groupHeader As GridGroupHeaderItem In _grid.MasterTableView.GetItems(GridItemType.GroupHeader)
                        Dim children As GridItem() = groupHeader.GetChildItems()
                        total = 0
                        For Each gridDataItem As GridItem In children
                            Dim item As GridDataItem = TryCast(gridDataItem, GridDataItem)
                            If TypeOf (item) Is GridDataItem Then
                                If item("exclude").Text <> "True" Then
                                    total = total + CDec(item(e.Column.UniqueName).Text)
                                End If
                            End If
                End If
            End If
            e.Result = total
        End Sub

  2. Konstantin Dikov
    Konstantin Dikov avatar
    2466 posts

    Posted 19 Jan 2017 Link to this post

    Hi Bryan,

    Here is a recursive solution for getting all GridDataItems:
    Protected Sub RadGrid1_CustomAggregate(sender As Object, e As GridCustomAggregateEventArgs)
        If TypeOf e.Item Is GridGroupFooterItem Then
            If e.Column.UniqueName = "ID" Then
                Dim dataItems = GetDataItems(TryCast(e.Item, GridGroupFooterItem).GroupHeaderItem)
                Dim sum As Double = 0
                For Each item As GridDataItem In dataItems
                    sum += Integer.Parse(item("ID").Text)
                e.Result = sum
            End If
        End If
    End Sub
    Private Function GetDataItems(header As GridGroupHeaderItem) As List(Of GridItem)
        Dim items = header.GetChildItems()
        Dim dataItems = New List(Of GridItem)()
        If items.Length > 0 AndAlso TypeOf items(0) Is GridDataItem Then
            Return items.ToList()
            For Each item As GridItem In items
                If TypeOf item Is GridGroupHeaderItem Then
                    dataItems.AddRange(GetDataItems(TryCast(item, GridGroupHeaderItem)))
                End If
            Return dataItems
        End If
    End Function

    C# Code:
    protected void RadGrid1_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
        if (e.Item is GridGroupFooterItem)
            if (e.Column.UniqueName == "ID")
                var dataItems = GetDataItems((e.Item as GridGroupFooterItem).GroupHeaderItem);
                double sum = 0;
                foreach (GridDataItem item in dataItems)
                    sum += int.Parse(item["ID"].Text);
                e.Result = sum;
    private List<GridItem> GetDataItems(GridGroupHeaderItem header)
        var items = header.GetChildItems();
        var dataItems = new List<GridItem>();
        if (items.Length > 0 && items[0] is GridDataItem)
            return items.ToList();
            foreach (GridItem item in items)
                if (item is GridGroupHeaderItem)
                    dataItems.AddRange(GetDataItems(item as GridGroupHeaderItem));                  
            return dataItems;

    Best Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top