Click here to Skip to main content
15,943,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
inside my gridview the columns are defined as follows

XML
<asp:TemplateField HeaderText="Amount" HeaderStyle-Width ="100px" FooterStyle-HorizontalAlign="right">
<ItemTemplate><%# Eval("amount")%></ItemTemplate>
</asp:TemplateField>


i want to get the total amount of all rows in my gridview for which i am using a for loop.
but i am unable to get the value of my amount column using findcontrol.
Posted

Hi,

Try the following


C#
decimal priceTotal = 0;
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
          priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
          "amount"));
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[0].Text = "Totals:";
        // for the Footer, display the running totals
        e.Row.Cells[1].Text = priceTotal.ToString("c");
    }
}



Hope this helps....
 
Share this answer
 
Comments
erogaki 12-Dec-11 23:58pm    
i want to calculate the total count on a button click....but am not able to get the value of the amount field using findcontrol
Hi
Try binding your amount using a label inside your ItemTemplate


XML
<asp:TemplateField HeaderText="Amount" HeaderStyle-Width="100px" FooterStyle-HorizontalAlign="right">
               <ItemTemplate>
               <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount")%>'></asp:Label></ItemTemplate>
                              </asp:TemplateField>



And on button click

C#
protected void btnTotal_Click(object sender, EventArgs e)
       {
           foreach (GridViewRow row in yourGridID.Rows)
           {
               Total+=double.Parse(((Label)row.FindControl("lblAmount")).Text);
           }

           yourLabelID.Text = Total.ToString();
       }



Hope this helps....
 
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