datagridview summary row windows forms
  • I have one Data Grid View I have to add the Rows cell value
     
          int val1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
          int val2 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value);

                dataGridView1.Rows[e.RowIndex].Cells[2].Value = val1 + val2;
    the above code which is used to add the sum of columns but how to add the sum of rows
  • 8 Comments sorted by
  • hi you can try this code for get column sum

    decimal Total = 0;
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
      Total+= Convert.ToDecimal(dataGridView1.Rows[i].Cells["ColumnName"].Value);
    }
    labelName.Text = Total.ToString();

  • please read my question clearly ithacker
  • Is this code Right?

    double total=0;
    int Column = BuildingDetailGrdView.CurrentCell.ColumnIndex;
    for (int i = 0; i < BuildingDetailGrdView.Rows.Count - 1; i++)
    {
    total += Convert.ToDouble(BuildingDetailGrdView.Rows[i].Cells[Column].Value);
    BuildingDetailGrdView.Rows[7].Cells[Column].Value = total;
    }
  • I thought that you want to get sum of column like

    ColumnName
    --------------------
    50
    100
    150
    200

    so my code will get sum as 500.

    so what you want? kindly explain with example.
  • s you are right but i want to show the answer at the last cell
    example
    Nature Col_Name1 Col_name2

    school 5 5
    class 2 3
    total 7 8
  • Vote Up0Vote Down ithackerithacker
    117.50 Karma Accepted Answer
    You can try this

    decimal Total = 0;
    for (int i = 0; i < dataGridView1.Rows.Count-2; i++)
    {
        Total+= Convert.ToDecimal(dataGridView1.Rows[i].Cells["ColumnName"].Value);
    }
    dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["ColumnName"].Value=Total; 

  • thank u its working
  • thank u its working Dear

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

Tagged

Popular Posts of the Week

Top Posters Weekly

Optimum Creative