Question

I'm populating a Telerik RadGrid with data from a DataTable (System.Data.DataTable). As I'm supporting someone else's application, I can't use any other data source or display control.

In my grid I have three columns: let's say they are Widgets Produced (column A), Faulty Widgets (column B) and Faultiness Proportion (column C). The database query provides these for me and does the calculation C = B / A.

I have a totals row (a Telerik GridFooterItem) at the bottom of the grid. In columns A and B Telerik calculates the totals for those columns for me. We can't calculate the correct value for the 'total' of column C from column C alone: we have to populate it with (the sum of B) / (the sum of A).

I've managed to do this by handling the DataBound event of the RadGrid and manually populating the cells in the footer. (I had to catch the GridFooterItem in the ItemCreated event, then put values in it in the DataBound event, after it had automatically calculated the totals for A and B for me.) This feels pretty hacky - maybe there's a better way...?

Anyway, the important bit is this:

My grid is split into groups so I also need to populate column C in the GridGroupFooterItems. I can't get my hacky technique to work in this case: I'm finding the footer cell I want with myGridFooterItem["WidgetsProduced"], but I can't get the group footer cells with myGridGroupFooterItem["WidgetsProduced"] - it just isn't a dictionary.

I've tried using myGridGroupFooterItem.Cells[], but this TableCellCollection contains a couple more cells than I'd expect, so accessing them by integer index feels a tad ropey (especially as this is a user-defined report, so the columns may be in any order).

So: how do I populate these cells with my calculation?

Was it helpful?

Solution

your item databound event looks something like

grd_ItemDataBound(object sender,GridItemEventArgs e)
{
//catch the footer element
if(e.Item.ItemType==GridItemType.GroupFooter)
{
    (e.Item.FindControl("yourTextBox") as TextBox).Text = your calculated value
}


}

OTHER TIPS

I double-checked whether unique name indexer works with Q2 2009 and Q3 2009 version of RadGrid for ASP.NET AJAX and it worked on my machine. Check your version and ask for more help using the Telerik forums if needed.

Dick

Try this

In Aspx page:

<telerik:GridBoundColumn Aggregate="Sum" DataField="Price" DataFormatString="{0:C}" EmptyDataText="0" FooterText="Total :" HeaderText="Price" UniqueName="Price"> ...

(Or)

In Code behind page:

Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound       
    Dim Item As GridDataItem
    Dim value as Double
    Select Case (e.Item.ItemType)
    Case Telerik.Web.UI.GridItemType.AlternatingItem, Telerik.Web.UI.GridItemType.EditItem,Telerik.Web.UI.GridItemType.Item,Telerik.Web.UI.GridItemType.SelectedItem

        Item = e.Item
    '------ Calculate Total  amount -----------
        Item("TotalPayment").Text = CDbl(Item("TotalPayment").Text) 
        value += CDec(Item("TotalPayment").Text)

    Case Telerik.Web.UI.GridItemType.Footer
        '------ Display the total  amount in Footer ------
        Dim footerItem As GridFooterItem = e.Item
        If Not RadGrid1.Items.Count = 0 Then footerItem("TotalPayment").Text = "Total :" + value
    End Select
End Sub

Well, if you use custom formula to calculate results instead of standard aggregates like sum, average and so on, you probably have to rely on your own logic. The cells from the group footers should be indexable by unique name - I remember this was not supported with previous Telerik ASP.NET Grid versions but this was added in more recent versions - Q2 or Q3 2009.

Dick

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top