locked
Extra row in gridview footer RRS feed

  • Question

  • User1487175000 posted

    Hi,

    I have a gridview and showfooter properties is enable. I want to add one extra row to the footer. So i did like this. I debug the solution its added the new row to the gridview but when gridview binded it does empty footer row.

                else if (e.Row.RowType.Equals(DataControlRowType.Footer))
                {
                    if (KONF.Checked)
                    {
                        budget bb = new budget(ddLocation.SelectedValue, ddBuilding.SelectedValue);
                        string budgetAmount = bb.calculateBudget(txtStartDate.Text, txtEndDate.Text);
                        TableRow tableRow = new TableRow();
                        TableCell cell0 = new TableCell();
                        TableCell cell1 = new TableCell();
                        TableCell cell2 = new TableCell();
                        TableCell cell3 = new TableCell();
                        TableCell cell4 = new TableCell();
                        TableCell cell5 = new TableCell();
                        TableCell cell6 = new TableCell();
                        cell0.Text = string.Empty;
                        cell1.Text = "Budget";
                        cell2.Text = string.Empty;
                        cell3.Text = string.Empty;
                        cell4.Text = string.Empty;
                        cell5.Text = budgetAmount;
                        cell6.Text = string.Empty;
                        tableRow.Controls.Add(cell0); 
                        tableRow.Controls.Add(cell1); 
                        tableRow.Controls.Add(cell2);
                        tableRow.Controls.Add(cell3); 
                        tableRow.Controls.Add(cell4); 
                        tableRow.Controls.Add(cell5);
                        tableRow.Controls.Add(cell6);
                        e.Row.NamingContainer.Controls.Add(tableRow);
                    }
                }

    Thursday, December 15, 2016 10:56 PM

All replies

  • User1724605321 posted

    Hi,

    If you want to add additional rows to the footer, you have to catch hold of the OnRowDataBound Event . Code below is for your reference :

            protected void cartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    TableRow tableRow = new TableRow();
                    TableCell cell0 = new TableCell();
                    TableCell cell1 = new TableCell();
                    TableCell cell2 = new TableCell();
                    TableCell cell3 = new TableCell();
                    TableCell cell4 = new TableCell();
                    TableCell cell5 = new TableCell();
                    TableCell cell6 = new TableCell();
                    cell0.Text = string.Empty;
                    cell1.Text = "Budget";
                    cell2.Text = string.Empty;
                    cell3.Text = string.Empty;
                    cell4.Text = string.Empty;
                    cell5.Text = "123";
                    cell6.Text = string.Empty;
                    tableRow.Controls.Add(cell0);
                    tableRow.Controls.Add(cell1);
                    tableRow.Controls.Add(cell2);
                    tableRow.Controls.Add(cell3);
                    tableRow.Controls.Add(cell4);
                    tableRow.Controls.Add(cell5);
                    tableRow.Controls.Add(cell6);
                    e.Row.NamingContainer.Controls.Add(tableRow);
                    
                }
            }
                    
    

    Markup:

    <asp:GridView ID="GridView1" CssClass="Grid" runat="server" AutoGenerateColumns="false"                
                    OnRowDataBound="cartGrid_RowDataBound"  ShowFooter="true" PageSize="10">
    

    Best Regards,

    Nan Yu

    Friday, December 16, 2016 8:12 AM
  • User1487175000 posted

    This is exactly same code what i have in my gridview rowdatabound event handler. But it shows empty footer.

    gridvie markup

                            <asp:GridView ID="gvReportWithDebit" runat="server" AutoGenerateColumns="False" Width="90%"
                                Visible="False" DataKeyNames="ID" ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="gvReportWithDebit_RowDataBound">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <img alt="" style="cursor: pointer" src="images/plus.png" class="expand"/>
                                            <asp:Panel ID="pnlOrders" runat="server"  Style="display: none;">
                                                <asp:GridView ID="gvNestedReportWithDebit" runat="server" AutoGenerateColumns="False" Width="100%"
                                                    DataKeyNames="BookingID,Datum" OnRowDataBound="gvNestedReportWithDebit_RowDataBound">
                                                    <Columns>
                                                        <asp:TemplateField HeaderText="Datum">
                                                            <ItemTemplate>
                                                                <asp:HyperLink ID="btnShowBooking" runat="server" Text='<%# Bind("Datum") %>'></asp:HyperLink>
                                                            </ItemTemplate>
                                                            <ControlStyle Width="100%" />
                                                            <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" />
                                                            <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
                                                        </asp:TemplateField>
                                                        <asp:BoundField DataField="Kund" HeaderText="Kund">
                                                            <ControlStyle Width="100%" />
                                                            <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" />
                                                            <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                                                            <ItemStyle HorizontalAlign="left" VerticalAlign="Top" />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Confirm" HeaderText="Confirm" DataFormatString="{0:N}">
                                                            <ControlStyle Width="100%" />
                                                            <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                            <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                                            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Preliminary" HeaderText="Preliminary" DataFormatString="{0:N}">
                                                            <ControlStyle Width="100%" />
                                                            <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                            <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                                            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:N}">
                                                            <ControlStyle Width="100%" />
                                                            <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                            <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                                            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                        </asp:BoundField>
                                                        <asp:TemplateField HeaderText="Debiterat">
                                                            <ItemTemplate>
                                                                <asp:HyperLink ID="btnShowDebit" runat="server"></asp:HyperLink>
                                                            </ItemTemplate>
                                                            <ControlStyle Width="100%" />
                                                            <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                            <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                                            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                                        </asp:TemplateField>
                                                    </Columns>
                                                </asp:GridView>
                                            </asp:Panel>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="Konferensbokare" HeaderText="Konferensbokare">
                                        <ControlStyle Width="100%" />
                                        <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" />
                                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Antal" HeaderText="Antal">
                                        <ControlStyle Width="100%"/>
                                        <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle>
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Confirm" HeaderText="Confirm" DataFormatString="{0:N}">
                                        <ControlStyle Width="100%" />
                                        <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle>
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Preliminary" HeaderText="Preliminary" DataFormatString="{0:N}">
                                        <ControlStyle Width="100%" />
                                        <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle>
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Belopp" HeaderText="Belopp" DataFormatString="{0:N}">
                                        <ControlStyle Width="100%" />
                                        <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle>
                                    </asp:BoundField>
                                    <asp:BoundField HeaderText="Debited" DataFormatString="{0:N}">
                                        <ControlStyle Width="100%" />
                                        <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" />
                                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle>
                                    </asp:BoundField>
                                </Columns>
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            </asp:GridView>

    RowDataBound

        protected void gvReportWithDebit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType.Equals(DataControlRowType.DataRow))
                {
                    st = new debitStatistics(txtStartDate.Text, txtEndDate.Text, ddLocation.SelectedValue, ddBuilding.SelectedValue, "conferenceBookerDetail", conferenceBookerID);
                    GridView nestedGridView = (GridView)e.Row.FindControl("gvNestedReportWithDebit");
                    nestedGridView.DataSource = st.StatisticsData;
                    nestedGridView.DataBind();
                }
                else if (e.Row.RowType.Equals(DataControlRowType.Footer))
                {
                    if (KONF.Checked)
                    {
                        budget bb = new budget(ddLocation.SelectedValue, ddBuilding.SelectedValue);
                        string budgetAmount = bb.calculateBudget(txtStartDate.Text, txtEndDate.Text);
                        TableRow tableRow = new TableRow();
                        TableCell cell0 = new TableCell();
                        TableCell cell1 = new TableCell();
                        TableCell cell2 = new TableCell();
                        TableCell cell3 = new TableCell();
                        TableCell cell4 = new TableCell();
                        TableCell cell5 = new TableCell();
                        TableCell cell6 = new TableCell();
                        cell0.Text = string.Empty;
                        cell1.Text = "Budget";
                        cell2.Text = string.Empty;
                        cell3.Text = string.Empty;
                        cell4.Text = string.Empty;
                        cell5.Text = budgetAmount;
                        cell6.Text = string.Empty;
                        tableRow.Controls.Add(cell0); 
                        tableRow.Controls.Add(cell1); 
                        tableRow.Controls.Add(cell2);
                        tableRow.Controls.Add(cell3); 
                        tableRow.Controls.Add(cell4); 
                        tableRow.Controls.Add(cell5);
                        tableRow.Controls.Add(cell6);
                        e.Row.NamingContainer.Controls.Add(tableRow);
                    }
                }
            }
            catch (Exception exc)
            {
                LOG.Error("Exception in Statistik gvReportWithDebit_RowDataBound: " + exc.StackTrace);
            }
        }

    Friday, December 16, 2016 9:06 AM
  • User-693045842 posted

    when binding the footer , you could refer to below article:

    https://msdn.microsoft.com/en-us/library/bb310552.aspx

    When use your code , the added row will show , but you didn't bind the footer .

    Thursday, December 29, 2016 9:09 AM