How to add the sum of rows value at the load time
  • i have one grid view it all the cells has some values  at the load time in last cell i need to add the rows value 

    eg

    std BC MBC SC ST total
    1 5 10 12 12 ?

  • 3 Comments sorted by
  • get idea from my below code i am using northwind database for sample.





    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=Northwind;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("select * from [Order Details]", con);
    sda.Fill(dt);
    dt.Columns.Add("Total",typeof(decimal));

    for (int i = 0; i < dt.Rows.Count; i++)
    {
    dt.Rows[i]["Total"] = Convert.ToDecimal(dt.Rows[i]["UnitPrice"]) + Convert.ToDecimal(dt.Rows[i]["Quantity"]);
    }

    dataGridView1.DataSource = dt;
  • i hope your syntax is not right

    for (int i = 0; i < dt.Rows.Count; i++)
    {
    StuGrdView.Rows[i].Cells["Total"]=Convert.ToDecimal(dt.Rows[i.cell]["UnitPrice"]) + Convert.ToDecimal(dt.Rows[i].cell["Quantity"]).Value;
    }
  • waqas i tried your codes its really help my code
    finally i found it
    for (int j = 0; j < StuGrdView.Rows.Count; j++)
    {
    int Total =0;
    for (int i = 1; i < StuGrdView.Columns.Count - 1; i++)
    {
    Total+= Convert.ToInt32(StuGrdView.Rows[j].Cells[i].Value);
    }
    StuGrdView.Rows[j].Cells["Total"].Value = Total.ToString();
    }
    thank you waqas

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Top Posters Weekly

Optimum Creative