none
How to Display Sum Total in the Footer of the DatagridView RRS feed

  • Question

  • hi all

    i have windows application in which a form containing datagridview is there.

    in that datagridview i want to show total of values of one column at last row .

    How can i do that
    • Moved by CoolDadTx Wednesday, November 20, 2019 2:36 PM Winforms related
    Friday, July 2, 2010 7:25 AM

Answers

  • I thought you wanted to ignore the null values. Here you're throwing a FormatException.

    void dataGridView1_DataSourceChanged(object sender, EventArgs e)
    {
     int sum = 0;
     for (int i = 0; i < dataGridView1.Rows.Count-2; i++)
     {
      if(dataGridView1[2, i].Value != DBNull.Value)
       sum += (int)dataGridView1[2, i].Value;
     }
     dataGridView1[2, dataGridView1.Rows.Count - 1].Value = sum;
    }
    
    • Marked as answer by Babak_bsn Tuesday, July 13, 2010 8:53 PM
    Tuesday, July 13, 2010 7:57 PM
  • no no no  ...  painting event handler  may be usefull  in  onother  case  but in this case  if you really want to compute the sum  of  all cells  you  must use the DataSourceChanged  event and

    void

     

     

    dataGridView1_DataSourceChanged(object sender, EventArgs e)

    {

     

     

    int sum = 0;

     

     

    for (int i = 0; i < dataGridView1.Rows.Count-2; i++)

    {

    sum +=

     

    int.Parse(dataGridView1[2, i].Value.ToString()) ;

    }

    dataGridView1[2, dataGridView1.Rows.Count - 1].Value = sum;

    }

    for harry zhu try  to put a break  point to see how many  time the paint event is invoked ; 


    A man's dreams are an index to his greatness
    • Marked as answer by Babak_bsn Tuesday, July 13, 2010 8:53 PM
    Tuesday, July 13, 2010 12:17 PM

All replies

  • create a int or decimal type variable

    private Decimal total = 0;

    and on focusedrow changed event code like this-

    total += Convert.ToDecimal(this.datagridview1.FocusedRow["column name"])

     

    and set this value in last row

     

    regards

    jayant

    Friday, July 2, 2010 7:42 AM
  • Thanks jayant

    but i have no FocusedRow property in my Datagridview

     

    Friday, July 2, 2010 7:48 AM
  • oops sorry dear

    actually i am using devexpress tool and i have post the code for devexpress component, in your case you can follow this-

    on rowLeave event

    total += Convert.ToDecimal(this.dataGridView.CurrentRow.Cells["column name"]);

     

    regards

    jayant

    Friday, July 2, 2010 7:57 AM
  • Thanks

    but this error occured

    Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.IConvertible'.

    Friday, July 2, 2010 8:03 AM
  • could you please post your code, i want to see that how you convert your cell value.

     

    regards

    jayant

    Friday, July 2, 2010 8:07 AM
  • decimal total=0;
          SqlCommand cmd = new SqlCommand("SELECT * FROM City",new Setting().Getconn());
          SqlDataReader dr = cmd.ExecuteReader();
          DataTable dt = new DataTable();
          dt.Load(dr);
          DataView dv = new DataView(dt);
          dataGridView1.DataSource = dv.ToTable("Table1", false, "CID", "Name");
          dataGridView2.DataSource = dv.ToTable("Table2", false, "tarikh");
          total += Convert.ToDecimal(dataGridView1.CurrentRow.Cells[0]);
          MessageBox.Show(total.ToString());

    Friday, July 2, 2010 8:09 AM
  • what is the data type of cell[0]..?


    regards

    jayant

    Friday, July 2, 2010 8:58 AM
  • integer
    Friday, July 2, 2010 9:40 AM
  • ok

    change your line -

    total += Convert.ToDecimal(dataGridView1.CurrentRow.Cells[0]);

    with this-

    total += Convert.ToDecimal(dataGridView1.CurrentRow.Cells[0].Value);

     

    regards

    jayant

    Monday, July 5, 2010 5:06 AM
  • try to  do  something  like  this  

     int sum = 0; 
    
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
            int partialsum = 0 ; 
    
            bool tryParse = int.TryParse(row.Cells[0].Value.ToString() ,out partialsum );
            if (tryParse)
            {
              sum += partialsum; 
              
            }
            
            
          }
    please try to describe more your intent if you find any issue  ...!
    A man's dreams are an index to his greatness
    Monday, July 5, 2010 6:35 AM
  • If the row.Cells[0].Value is an integer, there's no need to parse the result of ToString().
    Monday, July 5, 2010 7:06 PM
  • hello

     

    now try this

    int tot=0;
    For(int i = 0 ; i<DataGridView1.RowCount - 1;i++)
    { tot += DataGridView1.Rows(i).Cells(2).Value
    }
    store the value of tot in last row

    • Proposed as answer by techmania Monday, July 5, 2010 7:42 PM
    Monday, July 5, 2010 7:41 PM
  • If the row.Cells[0].Value is an integer, there's no need to parse the result of ToString().

    it's the best practice to  be sure that all cells with  a null  a value will  be ignored if  there is a cell with  a null  value 
    A man's dreams are an index to his greatness
    Tuesday, July 6, 2010 6:26 AM
  • Best practice to ignore null values is to ignore null values.

     

    object o = row.Cells[0].Value;

    if(o != DBNull.Value) ...

    Wednesday, July 7, 2010 1:18 AM
  • Hi,

    You may total the value in cell painting event handler:

    Here is a thread relating the same question:
    http://www.developmentnow.com/g/31_2008_8_0_0_831733/DataGridView-Summary-Row.htm

    Harry


    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.
    • Marked as answer by Babak_bsn Monday, July 12, 2010 6:28 AM
    • Unmarked as answer by Babak_bsn Tuesday, July 13, 2010 8:51 PM
    Monday, July 12, 2010 6:17 AM
  • no no no  ...  painting event handler  may be usefull  in  onother  case  but in this case  if you really want to compute the sum  of  all cells  you  must use the DataSourceChanged  event and

    void

     

     

    dataGridView1_DataSourceChanged(object sender, EventArgs e)

    {

     

     

    int sum = 0;

     

     

    for (int i = 0; i < dataGridView1.Rows.Count-2; i++)

    {

    sum +=

     

    int.Parse(dataGridView1[2, i].Value.ToString()) ;

    }

    dataGridView1[2, dataGridView1.Rows.Count - 1].Value = sum;

    }

    for harry zhu try  to put a break  point to see how many  time the paint event is invoked ; 


    A man's dreams are an index to his greatness
    • Marked as answer by Babak_bsn Tuesday, July 13, 2010 8:53 PM
    Tuesday, July 13, 2010 12:17 PM
  • I thought you wanted to ignore the null values. Here you're throwing a FormatException.

    void dataGridView1_DataSourceChanged(object sender, EventArgs e)
    {
     int sum = 0;
     for (int i = 0; i < dataGridView1.Rows.Count-2; i++)
     {
      if(dataGridView1[2, i].Value != DBNull.Value)
       sum += (int)dataGridView1[2, i].Value;
     }
     dataGridView1[2, dataGridView1.Rows.Count - 1].Value = sum;
    }
    
    • Marked as answer by Babak_bsn Tuesday, July 13, 2010 8:53 PM
    Tuesday, July 13, 2010 7:57 PM
  • Thank's friends

    your posts are very useful for me

    Tuesday, July 13, 2010 8:55 PM
  • Hey Jayant Try this

     private double Total()
            {
                double tot = 0;
                int i = 0;
                for (i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    tot = tot + Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value);
                }
                return tot;
            }

    label4.Text= Total().ToString("c");

    Thanks


    somillohani

    Sunday, June 30, 2013 5:21 PM
  • See this link--

    https://www.youtube.com/watch?v=Erhvmag787M

    Wednesday, November 20, 2019 10:38 AM