Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
check my footer summary code , I don't know what is the problem please help me.

C#
protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();

            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label WORKHRS = (Label)e.Row.FindControl("WORKHRS");
                int qty = Int32.Parse(WORKHRS.Text);
                total = total + qty;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblTotalhours = (Label)e.Row.FindControl("WORKHRS");
                lblTotalhours.Text = total.ToString();
            }
        }


--------------------------------------------------------------

XML
<asp:GridView ID="grvList" runat="server" AutoGenerateColumns="False" CssClass="gridview" GridLines="Horizontal" ShowFooter="true"  OnRowDataBound="grvList_RowDataBound" PageSize="5">
       <AlternatingRowStyle CssClass="alternate" />
       <Columns>
           <asp:BoundField HeaderText="Sl. No."></asp:BoundField>
           <asp:BoundField DataField="WorkDate" DataFormatString="{0:dd-MMM-yyyy}" HeaderText="WorkDate">
               <HeaderStyle Width="150px" />
           </asp:BoundField>
           <asp:BoundField DataField="InTime" DataFormatString="{0:HH:mm tt}" HeaderText="In Time">
               <HeaderStyle Width="150px" />
           </asp:BoundField>
           <asp:BoundField DataField="OutTime" DataFormatString="{0:HH:mm tt}" HeaderText="Out Time">
               <HeaderStyle Width="150px" />
           </asp:BoundField>


           <%--<asp:BoundField DataField="WORKHRS" HeaderText="Hours"></asp:BoundField>--%>
         <asp:TemplateField HeaderText="WORKHRS">
        <ItemTemplate>
           <div>
           <asp:Label ID="WORKHRS" runat="server" Text='<%# Bind("WORKHRS") %>' />
           </div>
        </ItemTemplate>
        <FooterTemplate>
           <div style="text-align: right;">
           <asp:Label ID="lblTotalhours" runat="server" />
           </div>
        </FooterTemplate>
     </asp:TemplateField>


       </Columns>
    <FooterStyle BackColor="#cccccc" Font-Bold="True" ForeColor="Black" HorizontalAlign="Left" />
       <EmptyDataTemplate>
           <div class="gridviewblank">Oops!. There is no data to display.</div>
       </EmptyDataTemplate>

   </asp:GridView>
Posted
Comments
Robert Welliever 30-Oct-14 22:42pm    
Why do you have three if statements in a row when e.Row.RowType can only be one thing? Try if-else statements. Also the first two if statements are checking for the same condition which is just goofy. However, the reason it won't work is when you check for the footer, you try to find a control that doesn't exist inside the footer template. The invalidly named "WORKHRS" control doesn't exist in the footer, which is why it doesn't work.
tastini 31-Oct-14 1:53am    
Can you edit the code for me. I try to change the variables and binding values still not give me answer. Thanks.

Reason is :You are finding a Label which is not defined in footer template. You defined "lblTotalhours" Label in footer but finding "WorkHrs"

Here is your solution:
protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
 
            }
 
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label WORKHRS = (Label)e.Row.FindControl("WORKHRS");
                int qty = Int32.Parse(WORKHRS.Text);
                total = total + qty;
            }
            else (e.Row.RowType == DataControlRowType.Footer)
            {
               // here you are finding a Label which is not defined in footer template. 
               
                Label lblTotalhours = (Label)e.Row.FindControl("lblTotalhours");
                lblTotalhours.Text = total.ToString();
            }
        }
 
Share this answer
 
Comments
King Fisher 31-Oct-14 8:48am    
Good Catch my 5+
Schatak 1-Nov-14 2:07am    
Thanks :)
VC.J 31-Oct-14 10:33am    
your answer is right I know
may be I am wrong just for curiosity I am asking this
the above code can be like this
protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
Label WORKHRS = (Label)e.Row.FindControl("WORKHRS");
int qty = Int32.Parse(WORKHRS.Text);
total = total + qty;
}

else (e.Row.RowType == DataControlRowType.Footer)
{
// here you are finding a Label which is not defined in footer template.

Label lblTotalhours = (Label)e.Row.FindControl("lblTotalhours");
lblTotalhours.Text = total.ToString();
}
}
Schatak 1-Nov-14 2:10am    
Yes you are right. there is extra if statement which can be removed.
Thanks
tastini 1-Nov-14 3:15am    
its also not working *Schatak .
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      float grand_total=0;
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();

          }
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          Label WORKHRS = (Label)e.Row.FindControl("WORKHRS");


          grand_total += float.Parse(WORKHRS.Text);

      }
      if (e.Row.RowType == DataControlRowType.Footer)
      {
          Label WORKHRS1= (Label)e.Row.FindControl("WORKHRS1");
          WORKHRS1.Text = grand_total.ToString();
      }
  }
 
Share this answer
 
Comments
Rajesh waran 31-Oct-14 9:13am    
if (e.Row.RowType == DataControlRowType.Footer)
{
Label WORKHRS1= (Label)e.Row.FindControl("WORKHRS1");
WORKHRS1.Text = grand_total.ToString();
}

In footer You are taking WORKHRS1 label,But Check the footer it consists of label which is named as lblTotalhours, so finding WORKHRS1 is not a right way.
Rajesh waran 31-Oct-14 9:16am    
Refer solution1 from @Schatak for further clarification
tastini 31-Oct-14 13:14pm    
Its not accepting still showing the error , "Additional information: Input string was not in a correct format."
tastini 31-Oct-14 16:56pm    
I try to edit this way... but still not succeeded anybody can help me to solve it fully....

if (e.Row.RowType == DataControlRowType.DataRow)
{
grTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "WORKHRS"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Attributes.Add("class", "tfooter");
e.Row.Cells[3].Text = "Total Hours";
e.Row.Font.Bold = true;
((Label)e.Row.Cells[4].FindControl("lblTotal")).Text = grTotal.ToString();
}

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