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

RadGrid with 3 footers

2 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jens Olesen
Top achievements
Rank 1
Jens Olesen asked on 23 Oct 2010, 10:05 PM
Hi All,

I (may) have a problem with an application containing a radgrid.
Because I do not know the number of columns at designtime, I declared the radgrid in the aspx file without columns, and build the grid structure in the code behind (In the NeedDataSource() event after data is fetched from the database).

In order to create 3 footerrows, my plan was to create a footertemplate, but I have read that this must be done in the page_init event.
During page_init however, I do not know the number of columns to create .... so I was stuck here.

I may have found a solution that works (Inspired from another thread about 2 HeaderRows).
During Grid.PreRender i add additional footerrows. It seems to work ok, but I am not sure if this is reliable.

Any comments ?

Thanks.
 
Here is the code:
 
Protected Sub Grid_TimeSheet_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid_TimeSheet.PreRender
    AddFooterRow(1)
    AddFooterRow(2)
End Sub
 
Protected Sub AddFooterRow(ByVal marker As Integer)
    ' get the current footer
    Dim footer As GridItem() = Grid_TimeSheet.MasterTableView.GetItems(GridItemType.Footer)
    ' get the current TFoot element
    Dim foot As GridTFoot = CType(footer(0).Parent.Controls(0).Parent, GridTFoot)
    Dim LastFooterPos As Integer = 0
 
    For n = 0 To foot.Controls.Count - 1
        If TypeOf (foot.Controls(n)) Is GridFooterItem Then
            LastFooterPos = n
        End If
    Next
    Dim newFooterItem As GridFooterItem = New GridFooterItem(Grid_TimeSheet.MasterTableView, 0, 0)
    PopulateNewFooterItem(newFooterItem, foot.Controls(LastFooterPos), marker)
    foot.Controls.AddAt(LastFooterPos + 1, newFooterItem)
 
End Sub
 
Protected Sub PopulateNewFooterItem(ByVal footer As GridFooterItem, ByVal existingfooter As GridFooterItem, ByVal marker As Integer)
    For Each fc In existingfooter.Cells
        Dim new_fc As TableCell = New TableCell
        new_fc.Text = marker.ToString()
        footer.Cells.Add(new_fc)
    Next
 
End Sub


2 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 27 Oct 2010, 12:25 PM
Hello Jens,

Modifying the number of grid items and adding/removing items programmatically is not supported in RadGrid. As you are modifying the HTML rendered by RadGrid, you can expect breakage of various client-side functionality. Also, as you are creating additional controls too late in the page life cycle, you may run into issues with loading the ViewState on postback. Furthermore, any child controls will not fire postback events as controls are not yet created by the time of the postback event phase in the page life cycle.

The recommended approach for introducing changes to footer items is to modify the content in each footer item cell, not adding/removing grid items. You can use RadGrid's ItemCreated event to get the GridFooterItem instance that is created automatically.The item contains N+2 number of cells, where N is the number of data columns automatically created during databinding. The first 2 cells are for 2 columns RadGrid creates internally, so your data columns start from index 2.You can get each table cell in the footer item and add custom content inside:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridFooterItem)
    {
        for (int i = 2; i < e.Item.Cells.Count; i++)
        {
            TableCell cell = e.Item.Cells[i];
            //add content or controls to cell
        }
    }
}

This is the only supported approach for modifying grid items programmatically, and it ensures both proper ViewState management and postback event handling for controls dynamically added to grid cells.

Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jens Olesen
Top achievements
Rank 1
answered on 27 Oct 2010, 01:05 PM
Thanks a lot Veli !

Best regards

Jens Olesen
Tags
Grid
Asked by
Jens Olesen
Top achievements
Rank 1
Answers by
Veli
Telerik team
Jens Olesen
Top achievements
Rank 1
Share this question
or