Click here to Skip to main content
15,886,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I need to find the sum for total amount column in dynamic gridview textbox

<asp:TemplateField HeaderText="Total Amount">
  <ItemTemplate>
  <asp:TextBox ID="totaltb" runat="server"></asp:TextBox>                                
</ItemTemplate>                            
</asp:TemplateField>


TextBox box5 = (TextBox)ItemGv.Rows[rowIndex].Cells[4].FindControl("totaltb");


Please help

Thanks
Posted

Hey
you can add footer Template in the your GridView (ItemGv).
C#
<FooterTemplate>
     <asp:Label ID="lblsum" runat="server"></asp:Label>
 </FooterTemplate>


Also handle the RowDataBound() event for it.

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  decimal rTotal = Convert.ToDecimal
              (DataBinder.Eval(e.Row.DataItem, "totaltb"));
  grsum = grdTotal + rTotal ;
 }
 if (e.Row.RowType == DataControlRowType.Footer)
 {
  Label lblsum= (Label)e.Row.FindControl("lblsum");
  lblsum.Text = grsum.ToString("c");
 }
}
 
Share this answer
 
v2
Try this:
C#
int sum = 0;
TextBox txt;
foreach(GridViewRow row in ItemGv.Rows)
{
   txt = (TextBox)row.Cells[4].FindControl("totaltb");
   sum += Convert.ToInt32(txt.Text);
}


Regards,
Eduard
 
Share this answer
 
Comments
Lancy.net 9-Dec-11 0:14am    
Thanks Friend
[no name] 9-Dec-11 0:21am    
you are welcome :)

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