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

Accessing grid footer totals in code behind

5 Answers 249 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 30 Jun 2009, 10:32 AM
Hi

My grid has a column which has a SUM aggregate being applied to it. This figure is being displayed correctly in the grid footer. I would like to be able to access this grid total in the code behind so i can display its value elsewhere on the page.

For a group footer you can access an aggregate field like so:

Protected Sub gridJourneys_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) If TypeOf e.Item Is GridGroupFooterItem Then
            Dim item As GridGroupFooterItem = CType(e.Item, GridGroupFooterItem)
         
            Dim groupDataRow As DataRowView = CType(e.Item.DataItem, DataRowView)

           response.write (groupDataRow("Total"))

I tried this in a grid footer but it didn't work. I got the error Object reference not set to instance of object because the e.item.DataItem proeprty is null/nothing

   ElseIf TypeOf e.Item Is GridFooterItem Then
            Dim footerItem As GridFooterItem = CType(e.Item, GridFooterItem)
            Dim gridDataRow As DataRowView = CType(e.Item.DataItem, DataRowView)
         
            Throw New Exception(gridDataRow Is Nothing) <=========TRUE

How do I access the grid totals please?

I have already read this page in the help file
ms-help://telerik.aspnetajax.radcontrols.2008.Q3/telerik.aspnetajax.radgrid.2008.Q3/totals-in-grid-footers.html

In this method you have to find the total yourself by keeping a running total of the column in the itemdatabound event. I could do this but it does seem like a case of reinventing the wheel as the grid has obviously calculated the total and stored it somewhere. I just can't find it!

many thanks



5 Answers, 1 is accepted

Sort by
0
andieje
Top achievements
Rank 1
answered on 01 Jul 2009, 08:05 PM
Hi

I would really appreciate it if someone from tech support could look at this question as I notice other questions posted after this one have already been answered.

Many thanks in advance
0
Princy
Top achievements
Rank 1
answered on 02 Jul 2009, 10:35 AM
Hi,


Try the code snippet below to access the aggregate in the ItemDatabound event of your grid:
c#:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridFooterItem) 
        { 
            GridFooterItem footerItem = (GridFooterItem)e.Item; 
            string strtxt = footerItem["columnUniqueName"].Text; 
        }  
    } 
 

Thanks
Princy.
0
andieje
Top achievements
Rank 1
answered on 03 Jul 2009, 12:17 AM
Hi

What would be the unique column name of a sum aggregate in the grid footer row? I tried SUM but i got an error saying the column did not exist. When you access a sum aggregate field in a group footer it has the name SUM (unless you give it a field alias)

Here is my column with the aggregate sum on it.

<telerik:GridBoundColumn DataField="DistanceTravelled" DataFormatString="{0:F2}" HeaderText="Distance Travelled (miles)" UniqueName="DistanceTravelled" Aggregate="Sum" FooterAggregateFormatString="Total miles: {0:F2}"></telerik:GridBoundColumn>

I think the method you described on applies to bound columns and not aggregate fields in the footer.
0
Accepted
Princy
Top achievements
Rank 1
answered on 03 Jul 2009, 05:13 AM
Hi,

The columnUniqueName in your case would be DistanceTravelled. The aggregates would be displayed in the footer of the column for which the aggregate property is set and hence you would have to access the footer text of the bound column to retrieve the aggregates for that column.

Hope this answers your query..
Princy.
0
andieje
Top achievements
Rank 1
answered on 05 Jul 2009, 12:03 AM
Hi

It did not occur to me that the column name in the footer item would give you the aggregate value when in the grid footer you use the name of the aggregate. It's frustrating how the different rows have different access mechanisms, like, for example, the lack of unique indexingin the group footer.

Many thanks for your help
Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
andieje
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or