We help IT Professionals succeed at work.

SUM of column in datatable

DaveMon
DaveMon asked
on
3,675 Views
Last Modified: 2010-05-18
Hello Experts,  I have a question about a web project I am working on, particularly, I am trying so get the SUM of a column of my databound Gridview. It currently works fine and displays three columns, but i am having problems tallying the last column for a total.



   protected void btnStart_Click(object sender, EventArgs e)
    {
   
string sql = string.Format("SELECT DISTINCT OrdFile.Order#, OrdFile.Quote#, (QtFile.TotBoxSqFtWalls + QtFile.TotBoxSqFtTops + QtFile.TotBoxSqFtFlrs) FROM OrdFile INNER JOIN QtUnit ON (OrdFile.Quote# = QtUnit.Quote#) INNER JOIN QtFile ON (QtFile.Quote# = OrdFile.Quote#) AND (QtFile.QtBox# = OrdFile.QtBox#) WHERE OrdFile.DateSchProd BETWEEN {0} AND {1} AND OrdFile.Quote# = QtUnit.Quote#", startDate.Value, endDate.Value);
       
        DataSet ds1 = new DataSet();
        kpweb.DBService mydb = new kpweb.DBService();
        ds1 = mydb.runQTSQL(sql);

        GridView1.DataSource = ds1.Tables[0].DefaultView;
        GridView1.DataBind();


       if (ds1.Tables[0].Rows[0][0].ToString().ToUpper() != "NOTHING")
        {
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
          string sqlJobStatusHistory = string.Format("SELECT SUM(Column1) FROM ds1.Tables[0]", ds1.Tables[0].Rows[i][1].ToString(), ds1.Tables[0].Rows[i][2].ToString());

                using (SqlConnection con = new SqlConnection(constrSQL))
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(sqlJobStatusHistory, con))
                    {
                        try
                        {
                            sqlJobStatusHistory = cmd.ExecuteScalar().ToString();

                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.Footer)
        {

            e.Row.Cells[1].Text = string.Format("Total :");
        
         //  Heres where I want the total displayed
         // e.Row.Cells[2].Text = string.Format("Total :");

        }
    }

thanks for any help on this.

Dave
Comment
Watch Question

Commented:
In line
-->>>string sqlJobStatusHistory = string.Format("SELECT SUM(Column1) FROM ds1.Tables[0]", ds1.Tables[0].Rows[i][1].ToString(), ds1.Tables[0].Rows[i][2].ToString());

You have typo and too many parameters. Did you mean:
string sqlJobStatusHistory = string.Format("SELECT SUM(Column1) FROM {0}", ds1.Tables[0].TableName);

Commented:
You can use the Compute method on the DataTable.
ds1.Tables[0].Compute("Sum(mycolumn)");
- or - Since you are looping through all the rows, you could add them up as you go
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.