Click here to Skip to main content
16,018,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" CssClass="table table-striped" 
                            AutoGenerateColumns="False" HorizontalAlign="Center" ShowFooter="true" OnRowDataBound="GridView1_RowDataBound" 
                            FooterStyle-ForeColor="Maroon" FooterStyle-Font-Bold="true" FooterStyle-Font-Underline="true">

                            <Columns>

                                <asp:TemplateField ItemStyle-Height="20" HeaderText="BIL.">
                                    <ItemTemplate>
                                        <%# Container.DataItemIndex + 1 %>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Company ID" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" Visible="false">
                                    <ItemTemplate>
                                        <asp:Label ID="CompLbl" runat="server" Text='<%# Eval("CompId") %>' ForeColor="Maroon" Style="font-size: 12px;"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Date" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="DateLbl" runat="server" Text='<%# Eval("OrderDate") %>' ForeColor="Maroon" Style="font-size: 12px;"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Order No" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="DateLbl" runat="server" Text='<%# Eval("OrdNo") %>' ForeColor="Maroon" Style="font-size: 12px;"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Products" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="MealLbl" runat="server" Text='<%# Eval("Product").ToString().Replace("%0a","<br />").Replace("|","<br />").Replace(Environment.NewLine,"<br />") %>' ForeColor="Maroon" Style="font-size: 12px;"></asp:Label>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:Label ID="TotalLbl" runat="server" Font-Bold="true" Text="Total" />
                                    </FooterTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Daily Total (RM)" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <%--<asp:TextBox ID="txtTotal" runat="server" Text='<%# Eval("Total") %>' ForeColor="Maroon"></asp:TextBox>--%>
                                        <asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:Label ID="lblTotalqty" runat="server" Font-Bold="true" />
                                    </FooterTemplate>
                                </asp:TemplateField>

                            </Columns>

                            <EmptyDataTemplate>
                                No Record Found....!!!  
                            </EmptyDataTemplate>
                        </asp:GridView>


Above is my gridview design code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            decimal TotalSales = (decimal)0.0;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //TextBox mytotsale = (TextBox)e.Row.FindControl("txtTotal");

                Label mytotsale = (Label)e.Row.FindControl("lblTotal");

                if (mytotsale.Text != null)
                {
                    int startIndex = 2;
                    int endIndex = mytotsale.Text.Length - startIndex;
                    string NewTotSales = mytotsale.Text.Substring(startIndex, endIndex);

                    TotalSales += Convert.ToDecimal(NewTotSales);
                }
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblSum1 = (Label)e.Row.FindControl("lblTotalqty");
                lblSum1.Text = TotalSales.ToString("C2");
            }
        }


My Label totsale is always showing null value. If i comment the the entire code for rowdatabound then the data is viewable.

What I have tried:

Tried to remove the row databound and it works, tried to change to textbox but still doesnt work means at rowdatabound it gives null value.
Posted
Updated 19-Oct-21 22:02pm
Comments
Richard Deeming 13-Oct-21 8:10am    
You don't have a label called totsale anywhere in your code.

Also, you're using a local variable to store the total. That's not going to magically store the values from the previous calls to the method. Your footer row will always have a total of 0.
Member 15347263 13-Oct-21 22:55pm    
Hi there, im my grdiview i have the below



<asp:templatefield headertext="Daily Total (RM)" headerstyle-horizontalalign="Left" itemstyle-horizontalalign="Left">
<itemtemplate>
<%--<asp:textbox id="txtTotal" runat="server" text="<%# Eval("Total") %>" forecolor="Maroon">--%>
<asp:label id="lblTotal" runat="server" text="<%# Eval("Total") %>">


in my codebehind i have the below :

Label mytotsale = (Label)e.Row.FindControl("lblTotal");


mytotsale.Text is always giving me null value. Please advise.

1 solution

Try this way:

Label mytotsale = e.Row.FindControl("lblTotal") as Label;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900