简体   繁体   中英

Telerik Radgrid : adding another footer row

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 the Telerik 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.

After doing some searching on the Telerik docs, I'm fairly sure this is not just possible. In this question , for instance, a Telerik admin states clearly that " RadGrid is data-bound control and its items are created based on the records in its datasource and display the datasource data. Therefore in order to add new row in the grid, you need to add new record in its datasource and rebind the grid. "

So, to work around my problem, I've decided to remove the footer on my grid and add a brand-new grid underneath, which I'll bind to a dummy DataTable I'll create by code in my Page_Load event handler. The values will be either determined client-side through Javascript or simply injected into the rows of that dummy table.

If anyone ever finds a more elegant solution, I'm still interested to know about it! Right now, though, work has to go forward.

    int lastFooterPos;

    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.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);

                int k = 0;
                int l = 0;
                int n = 0;
                int p = 0;
                int a = 0;
                int b = 0;
                int c = 0;
                foreach (TableCell fc in existingFooter.Cells) {
                    //decimal cost = Convert.ToDecimal(existingFooter["Marks"].Text);
                    TableCell newFooterCell = new TableCell();
                    if (k == 0) {
                        newFooterCell.Text = "";
                        newFooterCell.Height = 12;
                        newFooterItem.Cells.Add(newFooterCell);
                        k = 1;

                    }
                    else {
                        if (l == 0) {
                            newFooterCell.Text = "";
                            newFooterCell.Height = 12;
                            newFooterItem.Cells.Add(newFooterCell);
                            l = 1;
                        }
                        else {
                            if (n == 0) {
                                newFooterCell.Text = "";
                                newFooterCell.Height = 12;
                                newFooterItem.Cells.Add(newFooterCell);
                                n = 1;
                            }
                            else {
                                if (p == 0) {
                                    newFooterCell.Text = "Another Total Footer:";
                                    newFooterCell.Height = 12;
                                    newFooterItem.Cells.Add(newFooterCell);
                                    p = 1;
                                }
                                else {
                                    if (a == 0) {
                                        newFooterCell.Text = Convert.ToString(existingFooter["Marks"].Text);
                                        newFooterCell.Height = 12;
                                        newFooterItem.Cells.Add(newFooterCell);
                                        a = 1;
                                    }
                                    else {
                                        if (b == 0) {
                                            newFooterCell.Text = Convert.ToString(existingFooter["Fees"].Text);
                                            newFooterCell.Height = 12;
                                            newFooterItem.Cells.Add(newFooterCell);
                                            b = 1;
                                        }
                                        else {
                                            if (c == 0) {
                                                newFooterCell.Text = Convert.ToString(existingFooter["Noofstudents"].Text);
                                                newFooterCell.Height = 12;
                                                newFooterItem.Cells.Add(newFooterCell);
                                                c = 1;
                                            }
                                            else {
                                                newFooterCell.Text = "";
                                                newFooterCell.Height = 12;
                                                newFooterItem.Cells.Add(newFooterCell);

                                            }


                                        }

                                    }

                                }

                            }
                        }
                    }

                    foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);

                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM