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

Custom Aggregate Multiple Groups

1 Answer 308 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bryan
Top achievements
Rank 1
Bryan asked on 16 Jan 2017, 01:26 PM

Hi

 

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
                    Next
                Next
            End If
        End If
        e.Result = total
    End Sub

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 19 Jan 2017, 07:06 AM
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)
            Next
            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()
    Else
        For Each item As GridItem In items
            If TypeOf item Is GridGroupHeaderItem Then
                dataItems.AddRange(GetDataItems(TryCast(item, GridGroupHeaderItem)))
            End If
        Next
        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();
    }
    else
    {
        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.
Tags
Grid
Asked by
Bryan
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or