locked
is it possible to make the last row of DataGridView as Sum the column ? RRS feed

  • Question

  • hi

    is it possible to make the last row of DataGridView as Sum the column ?

    and that the last row always will show ?

    (work on Visual-studio 2008, WinForm)

    thank's in advance

    Saturday, March 6, 2010 11:33 AM

Answers

  • Hi E_gold,

    You can bind your datagridview to a data table, calculate the sum of column, then add a new row to the data table with the sum value you got. Something like this:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private SqlDataAdapter da;
        private DataSet ds;
        private void Form1_Load(object sender, EventArgs e)
        {
            string strCnn = @"Data Source = .\SQLExpress; Initial Catalog = Northwind; Integrated Security = True;";
            string strSql = "Select ProductID, ProductName, UnitPrice From Products";
    
            SqlConnection cn = new SqlConnection(strCnn);
            da = new SqlDataAdapter(strSql, cn);
    
            ds = new DataSet();
            DataTable dt = ds.Tables.Add("Products");
    
            da.Fill(ds.Tables["Products"]);           
    
            DataRow dr = dt.NewRow();
    
            dr["UnitPrice"] = ds.Tables["Products"].Compute("SUM(UnitPrice)", String.Empty);
    
            dt.Rows.Add(dr);
    
            this.dataGridView1 .DataSource = dt; 
        }
    }
    


    Or you can refer to this useful link.
    http://code.seanhess.net/?p=17


    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Edited by Alex Liang Friday, March 12, 2010 3:26 AM Add a link
    • Marked as answer by Alex Liang Wednesday, March 17, 2010 3:59 AM
    Friday, March 12, 2010 3:22 AM

All replies

  • hi

    is it possible to make the last row of DataGridView as Sum the column ?

    and that the last row always will show ?

    (work on Visual-studio 2008, WinForm)

    thank's in advance

    • Merged by Jing0 Friday, March 12, 2010 9:41 AM Merge it to keep the same topic in one thread
    Saturday, March 6, 2010 11:33 AM
  • Hi

           One way I am seeing solution for you problem is to prepare your data set prior DataBind() event.
    I mean before you bind the data to your grid, prepare DataSet which you got from DB such that last row contains Sum of respective columns.

    Regards, Srikanth Gunnala
    Saturday, March 6, 2010 11:48 AM
  • Is your datagridview bound to a datasource?

    Saturday, March 6, 2010 12:04 PM
  • Hi E_gold,

    You can bind your datagridview to a data table, calculate the sum of column, then add a new row to the data table with the sum value you got. Something like this:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private SqlDataAdapter da;
        private DataSet ds;
        private void Form1_Load(object sender, EventArgs e)
        {
            string strCnn = @"Data Source = .\SQLExpress; Initial Catalog = Northwind; Integrated Security = True;";
            string strSql = "Select ProductID, ProductName, UnitPrice From Products";
    
            SqlConnection cn = new SqlConnection(strCnn);
            da = new SqlDataAdapter(strSql, cn);
    
            ds = new DataSet();
            DataTable dt = ds.Tables.Add("Products");
    
            da.Fill(ds.Tables["Products"]);           
    
            DataRow dr = dt.NewRow();
    
            dr["UnitPrice"] = ds.Tables["Products"].Compute("SUM(UnitPrice)", String.Empty);
    
            dt.Rows.Add(dr);
    
            this.dataGridView1 .DataSource = dt; 
        }
    }
    


    Or you can refer to this useful link.
    http://code.seanhess.net/?p=17


    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Edited by Alex Liang Friday, March 12, 2010 3:26 AM Add a link
    • Marked as answer by Alex Liang Wednesday, March 17, 2010 3:59 AM
    Friday, March 12, 2010 3:22 AM