views:

1934

answers:

3

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?

A: 

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

Dick Lampard
Should be... but aren't. :-(
teedyay
hmm, not sure why -1 . Can you explain ?
ram
@Ram - the tooltip for down-voting is "This answer is not useful", which I'm afraid describes this answer. In my question I'd said that accessing group footer cells using the UniqueName as an index doesn't work. Maybe this is fixed in later versions, but that doesn't help me, unfortunately.
teedyay
+1  A: 

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
}


}
ram
How do I put a TextBox (or a Literal, preferably) into that cell?
teedyay
you would use Footer Template in your gridfrom : example in http://www.telerik.com/help/aspnet-ajax/grdgridtotalswithradnumerictextbox.html <FooterTemplate> <telerik:RadNumericTextBox ID="TextBox2" runat="server"> <ClientEvents OnLoad="Load" /> </telerik:RadNumericTextBox> </FooterTemplate>
ram
Ah, of course! My grid's being generated dynamically so I'll have to figure out how to create the templates programmatically, but this looks like the way to go. Thanks.
teedyay
+1  A: 

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

Dick Lampard
Thanks - yeah, it looks like I'm on 2008 Q2. I'll see if I can get my hands on a newer version.
teedyay