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

How do I add multiple footer rows to a grid?

6 Answers 835 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 22 Mar 2013, 05:35 PM
Hi! I'm new to ASP and the web in general so bear with me.

I need to add multiple footer rows to my RadGrid instance ; for the moment, however, I just want to add second one. I have currently a single footer row and it's working and displaying perfectly, for the record.

I found the following relevant question on these  forums and tried implemeting it but it's not working : the code gets executed, and the new FooterItem gets added to the Controls but that second row just doesn't appear. I need to find out why and I'd be grateful if anyone could help me fix that problem.

ASP grid code

<div id="OrderMainContent">
    <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1" />
            <telerik:AjaxSetting AjaxControlID="txtQuantity">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadInputManager ID="RadInputManager1" runat="server">
        <telerik:NumericTextBoxSetting BehaviorID="NumericBehavior1" Type="Number" DecimalDigits="0">
            <TargetControls>
                <telerik:TargetInput ControlID="RadGrid1" />
            </TargetControls>
        </telerik:NumericTextBoxSetting>
    </telerik:RadInputManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Sunset" AllowSorting="True" AutoGenerateColumns="False"
        GridLines="None" ShowFooter="True" OnItemDataBound="RadGrid1_ItemDataBound" OnPreRender="RadGrid1_PreRender">
        <MasterTableView DataKeyNames="ProductID" TableLayout="Fixed">
            <RowIndicatorColumn>
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn>
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridBoundColumn UniqueName="colProduct" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblProduct %>"
                    HeaderStyle-HorizontalAlign="Center" DataField="ProdDesc">
                    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn UniqueName="colQuantity" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblQuantity %>"
                    HeaderStyle-HorizontalAlign="Center" DataField="OrderQty" ColumnEditorID="txtQuantity">
                    <HeaderStyle Width="90" />
                    <ItemStyle Width="90px" />
                    <ItemTemplate>
                        <asp:TextBox ID="txtQuantity" runat="server" Width="50px" OnTextChanged="txtQuantity_TextChanged"
                            AutoPostBack="true">
                        </asp:TextBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn UniqueName="colPrice" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblBasePrice %>"
                    HeaderStyle-HorizontalAlign="Center" DataField="ProdUnitPrice">
                    <HeaderStyle Width="80px" />
                    <ItemStyle Width="80px" />
                    <ItemTemplate>
                        <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("ProdUnitPrice") %>' />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn UniqueName="colNotes" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblNotes %>"
                    HeaderStyle-HorizontalAlign="Center">
                    <HeaderStyle Width="200px" />
                    <ItemStyle Width="200px" />
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
    </telerik:RadGrid>
</div>


Relevant code behind

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    AddFooterRow(sender as RadGrid);
}
 
private void AddFooterRow(RadGrid grid)
{
    if (grid != null)
    {
        GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
 
        if (footerItems.Count() == 1)
        {
            GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot;
 
            for (int i = 0; i < foot.Controls.Count; i++)
            {
                GridFooterItem item = foot.Controls[i] as GridFooterItem;
 
                if(item != null)
                {
                    lastFooterPos = i;
                    break;
                }
            }
 
            GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem;
            GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0);
 
            foreach(TableCell fc in existingFooter.Cells)
            {
                TableCell newFooterCell = new TableCell();
                newFooterCell.Text = "allo";
                newFooterItem.Cells.Add(newFooterCell);
            }
 
            foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);
        }
    }
}


Of course if you need more precisions, just ask. Thank you for your help.

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Mar 2013, 05:02 AM
Hi,

Unfortunately I cannot replicate the issue at my end. The footer row gets added with the following code. Attached is the screenshot.
C#:
int lastFooterPos;
    void RadGrid1_PreRender(object sender, EventArgs e)
    {
        AddFooterRow(sender as RadGrid);
    }
    
    private void AddFooterRow(RadGrid grid)
    {
        if (grid != null)
        {
            GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
 
            if (footerItems.Count() == 1)
            {
                GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot;
 
                for (int i = 0; i < foot.Controls.Count; i++)
                {
                    GridFooterItem item = foot.Controls[i] as GridFooterItem;
 
                    if (item != null)
                    {
                        lastFooterPos = i;
                        break;
                    }
                }
                GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem;
                GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0);
 
                foreach (TableCell fc in existingFooter.Cells)
                {
                    TableCell newFooterCell = new TableCell();
                    newFooterCell.Text = "allo";
                    newFooterItem.Cells.Add(newFooterCell);
                }
 
                foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);
            }
        }
    }

Thanks,
Shinu.
0
Mauro
Top achievements
Rank 1
answered on 12 Jul 2013, 03:55 PM
Hi Telerik team!

I have the same problem here with Q1.2013
I have added the cited code in PreRender() but the footer is not added in the rendered RadGrid.
Only the first GridFooterItem in GridTFoot.Controls[] is shown; the original one or one of the programmatically made if I change their order.

I have UseStaticHeaders enabled but I don't think it is an issue because in the same RadGrid I am adding multiple headers without problems.
0
Mallika
Top achievements
Rank 1
answered on 26 Jan 2018, 10:14 PM
this code is not working for me. I am not seeing any new footer row. Can you please suggest me different approach?
0
Mallika
Top achievements
Rank 1
answered on 26 Jan 2018, 10:15 PM
this code is not working for me. I am not seeing any new footer row. Can you please suggest me different approach?
0
MBEN
Top achievements
Rank 2
Veteran
answered on 13 Aug 2018, 07:48 PM

I want to add a footnote to my grid. I am trying to add another footer row using the approach above in my PreRender event but it doesn't show on my grid.

private void AddFooterRow(RadGrid grid)
    {
        if (grid != null)
        {
            GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
 
            if (footerItems.Length == 1)
            {
                GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot;
 
                for (int i = 0; i < foot.Controls.Count; i++)
                {
                    GridFooterItem item = foot.Controls[i] as GridFooterItem;
 
                    if (item != null)
                    {
                        lastFooterPos = i;
                        break;
                    }
                }
 
                //GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem;
                GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0);
 
                TableCell newFooterCell = new TableCell();
                newFooterCell.Text = "Second Footer ";
                newFooterItem.Cells.Add(newFooterCell);
 
                foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);
            }
        }
    }
0
Eyup
Telerik team
answered on 16 Aug 2018, 06:35 AM
Hello,

Generally, you can display the text in the footer cells in 2 lines:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Common/totals-in-grid-footers#multi-line-footer-totals

This is also demonstrated for the group footers in the attached web site sample.

There is also a template option for the group footers:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/grouping/group-header-and-footer-templates

If you want to place a common footer for the grid, you can use a GridCommandItem:
https://www.telerik.com/support/code-library/different-controls-in-the-top-and-bottom-commanditem-of-radgrid

Please bear in mind that modifying the table structure of the grid is not recommended since it may break its structure and may lead to unexpected issues.

I hope this will prove helpful.

Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mauro
Top achievements
Rank 1
Mallika
Top achievements
Rank 1
MBEN
Top achievements
Rank 2
Veteran
Eyup
Telerik team
Share this question
or