Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to total in footer of gridview but it doesnt work can any one helpme with code or any suggsion code for me..... plz Help...plz helpme with new code..

What I have tried:

public void BindGrid()
      {
          string com = "Select * From [dbo].[client]";
          string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
          using (SqlConnection con = new SqlConnection(constr))
          {
              using (SqlCommand cmd = new SqlCommand(com))
              {
                  using (SqlDataAdapter sda = new SqlDataAdapter())
                  {
                      cmd.Connection = con;
                      sda.SelectCommand = cmd;
                      using (DataTable dt = new DataTable())
                      {
                          sda.Fill(dt);
                          GridView1.DataSource = dt;
                          GridView1.DataBind();

                          //Calculate Sum and display in Footer Row
                          decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Rate"));
                          GridView1.FooterRow.Cells[3].Text = "Total";
                          GridView1.FooterRow.Cells[3].HorizontalAlign = HorizontalAlign.Right;
                          GridView1.FooterRow.Cells[4].Text = total.ToString("txl");
                      }
                  }
              }
          }
      }

This my TextBox value in gridview
foreach (GridViewRow row in GridView1.Rows)
    {
    TextBox TextBox1 = (TextBox)row.FindControl("TextBox1");
    TextBox TextBox2 = (TextBox)row.FindControl("TextBox2");
    TextBox TextBox3 = (TextBox)row.FindControl("TextBox3");
    TextBox TextBox4 = (TextBox)row.FindControl("TextBox4");
    TextBox TextBox5 = (TextBox)row.FindControl("TextBox5");
    TextBox txtnet = (TextBox)row.FindControl("txtnet");

    string SrNO = TextBox1.Text;
    string ProductID = TextBox2.Text;
    string Quntity = TextBox3.Text;
    string Rate = TextBox4.Text;
    string Total = TextBox5.Text;
Posted
Updated 3-Oct-20 3:51am
Comments
Richard MacCutchan 3-Oct-20 4:25am    
"but it doesnt work"
What is that supposed to mean? Please use the Improve question link above and explain exactly what is not happening and where in the code the problem occurs.

1 solution

What do you think this accomplishes?

GridView1.FooterRow.Cells[4].Text = total.ToString("txl");


You'll have better lick with this:

GridView1.FooterRow.Cells[4].Text = total.ToString();
 
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