Click here to Skip to main content
15,887,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to show grand total in datagridview footer but here im not using any database connection i want to show grandtotal without using any database connection for this i wrote code but its not working

What I have tried:

Note : im not using any database connection i want to show this total without any database connection
Two Coloumn total :
C#
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
      {
          double cell1 = 0;
          double cell2 = 0;

          if (e.ColumnIndex == 0 || e.ColumnIndex == 2)
          {
              if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[0].Value, DBNull.Value))
              {
                  cell1 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[0].Value);
              }

              if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[2].Value, DBNull.Value))
              {
                  cell2 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[2].Value);
              }
              if (cell1.ToString() != "" && cell2.ToString() != "")
              {

                  dataGridView1.CurrentRow.Cells[6].Value = cell1 * cell2;

              }

          }

      }


GrandTotal code :(this code is not working)
C#
private void dataGridView1_DataSourceChanged_1(object sender, EventArgs e)
        {
            double Total = 0;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                Total += Convert.ToDouble(dataGridView1.Rows[i].Cells["Rupees"].Value);
            }
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Rupees"].Value = Total;
            lblsum.Text = Convert.ToString(Total);

        }
Posted
Updated 20-Jun-16 22:20pm
Comments
LLLLGGGG 19-Jun-16 11:34am    
Have you tried debugging your code?
Atul Rokade 19-Jun-16 12:01pm    
Debugger not go inside datasource code :(
CHill60 19-Jun-16 13:22pm    
But it will go into the DataSourceChanged code - put a breakpoint on double Total = 0; and double cell1 = 0; If you don't hit those breakpoints then you haven't wired up those events properly (possible given the _1 part of the name). If you do hit the breakpoints then step through the code to work out why it is not updating a grand total row
Atul Rokade 20-Jun-16 4:50am    
it is going private void dataGridView1_CellEndEdit_1 event but its not go inside dataGridView1_DataSourceChanged_1 event i used the debug but debugger itself not going inside dataGridView1_DataSourceChanged_1 event
CHill60 20-Jun-16 4:58am    
How are you populating this grid?

You need to change the value in the backing data source, not in the grid itself.

Marc
 
Share this answer
 
Comments
Atul Rokade 20-Jun-16 4:48am    
Dear Marc can you elaborate please
Sorry it took me so long to respond to your reply to my comment.

So we've established that this is an empty dataGridView that the user will populate by typing in values. This is why the dataGridView1_DataSourceChanged_1 event is never fired. That event would only get fired if you did something like
C#
dataGridView1.DataSource = someDataTable;
which you are not going to do on a grid that is populated entirely by the User.

The CellEndEdit is the correct event to hook on to, to get your grand total.
This works:
C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    double cell1 = 0;   //Grand total of column 0
    double cell2 = 0;   //Grand total of column 2

    // Only interested in changes in columns 0 and 2 so ignore other columns
    if (e.ColumnIndex != 0 && e.ColumnIndex != 2) return;

    //Calculate grand total ... if only the total for this row is required
    // then comment out the foreach...
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        double c1 = 0;
        double c2 = 0;
        if (r.Cells[0].Value != null)
            if (double.TryParse(r.Cells[0].Value.ToString(), out c1))
                cell1 += c1;
        if (r.Cells[2].Value != null)
            if (double.TryParse(r.Cells[2].Value.ToString(), out c2))
                cell2 += c2;
    }
    // Insert the grand total into this row
    dataGridView1.Rows[e.RowIndex].Cells[6].Value = cell1 + cell2;

}
I've assumed you are only adding up values in columns 0 and 2 and the current row gets the current grand total, previous rows are not revisited to update the grand total.

The code you originally posted is working fine for me, except it is calculating the product (cell1 multiplied by cell2) per row
 
Share this answer
 
Comments
Atul Rokade 21-Jun-16 12:57pm    
thnx chill60 bro :)

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