Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have a Gridview where I used header templates. Now I want to calculate Grand total in Footer Template. The code is below:-

C#
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {

C#
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            servchg  += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "servicechg"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblservchgamount = (Label)e.Row.FindControl("lblservchg");
            lblservchgamount.Text = servchg.ToString();
        }


}

Now the problem is that I am getting blank Grandtotal column. When debugged I found out that condition "e.Row.RowType == DataControlRowType.DataRow" is bypassed.

Not found any example of a Gridview with both Header and Footer Template in Google .


Please post some, if anybody knows.


Thanks...
Posted
Comments
Karthik_Mahalingam 6-Jan-14 3:38am    
have u enabled ShowFooter = true ??
abatechcal_sreya 6-Jan-14 3:42am    
ya... certainly. else blank footer column won't be there..
Karthik_Mahalingam 6-Jan-14 3:46am    
what error u r getting?
r u getting some value in this line .lblservchgamount.Text = servchg.ToString(); ??
Anuja Pawar Indore 6-Jan-14 3:45am    
Refer similar thread
http://forums.asp.net/t/1920422.aspx?if+e+Row+RowType+DataControlRowType+DataRow+always+false
abatechcal_sreya 6-Jan-14 3:52am    
Thanks Anuja..but the thread is not closed and my query still remains:(

 
Share this answer
 
ASP.NET
//////it is in aspx page

 <asp:gridview id="gvDetails" runat="server" autogeneratecolumns="False" showfooter="true" datakeynames="id" onrowdatabound="gvDetails_RowDataBound"  />
 
<columns>
<asp:templatefield itemstyle-horizontalalign="Center" footerstyle-horizontalalign="Center" xmlns:asp="#unknown">
                                <HeaderTemplate>
                                    Service Charge</HeaderTemplate>
                                <HeaderStyle  Width="15%" />
                                
                                <itemtemplate>
                                    <asp:label id="lblServChrgRows" runat="server" Text='<%#Eval("servicechg") %>'/>
                                </itemtemplate>

                                <footertemplate>
                                    <div style="text-align:right; padding-removed20px;line-height:18px;">
                                    <asp:label id="lblservchg" runat="server"  />                                      </div></footertemplate></asp:templatefield></columns>
 </asp:GridView>



it is apsx.cs side code

C#
 decimal servchg = 0;
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
     if (e.Row.RowType == DataControlRowType.DataRow)
        {  Label lblServChrgRows= (Label)e.Row.FindControl("lblServChrgRows");
                 if (!string.IsNullOrEmpty(lblServChrgRows.text))
                 { 
                servchg += Convert.ToDecimal(lblServChrgRows.text);
                }

            
        }
       
        if (e.Row.RowType == DataControlRowType.Footer)
        { 
            Label lblservchgamount = (Label)e.Row.FindControl("lblservchg");
            lblservchgamount.Text = servchg.ToString();
        }
    }
 
Share this answer
 
v10
Comments
abatechcal_sreya 6-Jan-14 4:48am    
please provide the code behind also...
Renuka Ruke 6-Jan-14 4:50am    
reply if any problem
Renuka Ruke 6-Jan-14 7:19am    
servchg += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, ...........just try it
abatechcal_sreya 6-Jan-14 7:23am    
Hi,

Its solved...thanks to u all...Thanks Renuka and Anuja specially
Renuka Ruke 6-Jan-14 7:30am    
its my pleasure.
it useful to you than please accept the 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