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

How do the Built in Footer Aggregate Functions Work?

3 Answers 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Billy
Top achievements
Rank 2
Billy asked on 23 Sep 2009, 06:54 PM
I can tell a column to do a sum of all its values and the sum will be displayed in the footer fo the grid. How exactly does the grid do this? Is it using Linq? I ask because I need to do a custom aggregate, and in order to do this custom aggregate i need all of the values in the column for each group.

For example, if I could do something along the lines of "select all values from rad grid where column = e.column and group =  ??" that would be great. As you can see I'm not really sure how to know what group I am in either. I've seen examples using looping in the PreRender event but how can this be done in the CustomAggregate event?

Thanks!

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Sep 2009, 06:32 AM
Hi Billy Hasan,

I suppose you have grouping enabled in your grid, and so if you want to add custom group footer aggregate, then you can try the following code.

ASPX:
 
 . . . 
<telerik:GridBoundColumn DataField="Number" Aggregate="Custom" DataType="System.Int64" HeaderText="Number" 
    SortExpression="Number" UniqueName="Number"
</telerik:GridBoundColumn> 
 . . . 

C#:
 
protected void RadGrid1_CustomAggregate(object sender, GridCustomAggregateEventArgs e) 
    if (e.Item is GridGroupFooterItem && e.Column.UniqueName == "Number"// When grouping is enabled 
    {  
        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(); 
            int result = 0; 
            foreach (GridDataItem item in items) 
            { 
                result = Convert.ToInt32(item["Number"].Text) + result; 
            } 
            e.Result = result.ToString(); 
        } 
    } 
    if (e.Item is GridFooterItem && e.Column.UniqueName == "Number"// When grouping is not enabled  
    { 
        int result=0; 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            result = Convert.ToInt32(item["Number"].Text) + result; 
        } 
        e.Result = result.ToString(); 
    }         

-Shinu.
0
Billy
Top achievements
Rank 2
answered on 24 Sep 2009, 12:04 PM
Thanks for the info, I ended up keeping track of the values in OnItemDataBound and then doing what I needed to do in the CustomAggregate event.

So I guess no sweet way to use Linq against the grid then? Still curious how the internals work for the sum, avg, etc. functions work.
0
nFocus
Top achievements
Rank 1
answered on 28 Jul 2011, 01:32 AM
This worked super until I realized that it didn't work when I needed the RadGrid to do paging.  Back to the drawing board.

.... I went back to the drawing board and realized that the built in aggregates work the same way.  The example by Shinu worked fine but I would note that e.Item.GroupIndex is a string, not an int.  If you group by two or more different columns then !bang! "Input string in wrong format" due to the x_y_z_... format when grouping.  Thanks for the lead though!
Tags
Grid
Asked by
Billy
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Billy
Top achievements
Rank 2
nFocus
Top achievements
Rank 1
Share this question
or