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

Programmatically add aggregate to a grid column

4 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 09 Jul 2009, 09:00 PM
Hi

I guess the title says it all. Is it possible to programmatically add an aggregate to a grid column and programatically set the gridfooterformatstring?

thanks

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 10 Jul 2009, 07:13 AM
Hello,

Check out the folowing code snippet which shows how to set the aggregate function for a column and FooterFormatString for the grid:
c#:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
        { 
            if (col.UniqueName == "column"
            { 
                (col as GridBoundColumn).Aggregate = GridAggregateFunction.Count; 
                (col as GridBoundColumn).FooterAggregateFormatString = "{0:C2}"
            } 
        } 
    } 

Thanks
Princy.
0
andieje
Top achievements
Rank 1
answered on 14 Jul 2009, 03:09 PM
thanks
0
Chris
Top achievements
Rank 1
answered on 17 Feb 2011, 08:42 PM
I am trying to do the exact same thing.  My grid is bound to a SQL Datasource.  I do not know all of the columns in the dataset (the user defines what data is in their dataset), however, I want to implement something like the following:

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
For Each col As GridColumn In RadGrid1.MasterTableView.RenderColumns
If col.UniqueName = "PRODUCT_UNITS" Then
TryCast(col, GridBoundColumn).Aggregate = GridAggregateFunction.Avg
TryCast(col, GridBoundColumn).FooterAggregateFormatString = "<strong>{0} (Avg)</strong>"
End If
Next
nd Sub

If the grid contains the column "PRODUCT_UNITS", I want to add an aggregate to it automatically like above.  When I implement the code above, .NET errors and says "Cannot find column [PRODUCT_UNITS]".  I know for a fact this column is in the dataset and is bound in the grid (grid has AutoGenerateColumns=True).  If I do a response.write of the col.UniqueName in the For loop, it prints PRODUCT_UNITS.  If I comment out all of the code above, the grid draws properly (no aggregating) and one of the columns is PRODUCT_UNITS.

What am I doing wrong?
0
Chris
Top achievements
Rank 1
answered on 18 Feb 2011, 03:43 AM
I figured out my issue.  This post lead me to my answer:   http://www.telerik.com/community/forums/aspnet-ajax/grid/dynamic-columns-in-a-grid-and-grouping-aggregate-problem.aspx

I had to set UseAllDataFields="True" of the MasterTableView.  Also, instead of setting the aggregate function in ItemDataBound, I put it on ColumnCreated.  Not sure if one is better than the other, but for what I'm doing that is working for me.

Thanks,
Chris
Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
andieje
Top achievements
Rank 1
Chris
Top achievements
Rank 1
Share this question
or