Click here to Skip to main content
13,663,987 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I want to add row 1 and row 2 and get it displayed on row 3 but I am facing some errors can someone please help thanks


   private void button2_Click(object sender, EventArgs e)   /// Click button it will do the adding
{
    String a;    // total value for row 3 cell 0
    dataGridView1.Rows[1].Cells[0].Value.ToString();
    dataGridView1.Rows[2].Cells[0].Value.ToString();
    Convert.ToDouble(dataGridView1.Rows[1].Cells[0].Value);
    Convert.ToDouble(dataGridView1.Rows[2].Cells[0].Value);
    a = dataGridView1.Rows[1].Cells[0].Value + dataGridView1.Rows[1].Cells[0].Value;
    dataGridView1.Rows[3].Cells[0].Value = a;

    String b;// total value for row 3 cell 1
    dataGridView1.Rows[1].Cells[1].Value.ToString();
    dataGridView1.Rows[2].Cells[1].Value.ToString();
    b = dataGridView1.Rows[1].Cells[1].Value.ToString() + dataGridView1.Rows[1].Cells[1].Value.ToString();
    dataGridView1.Rows[3].Cells[1].Value = b;

    String c;// total value for row 3 cell 2
    dataGridView1.Rows[1].Cells[2].Value.ToString();
    dataGridView1.Rows[2].Cells[2].Value.ToString();
    c = dataGridView1.Rows[1].Cells[2].Value.ToString() + dataGridView1.Rows[1].Cells[2].Value.ToString();
    dataGridView1.Rows[3].Cells[2].Value = c;

    String d;// total value for row 3 cell 3
    dataGridView1.Rows[1].Cells[3].Value.ToString();
    dataGridView1.Rows[2].Cells[3].Value.ToString();
    d = dataGridView1.Rows[1].Cells[3].Value.ToString() + dataGridView1.Rows[1].Cells[3].Value.ToString();
    dataGridView1.Rows[3].Cells[3].Value = d;

    String f;// total value for row 3 cell 4
    dataGridView1.Rows[1].Cells[4].Value.ToString();
    dataGridView1.Rows[2].Cells[4].Value.ToString();
    f = dataGridView1.Rows[1].Cells[4].Value.ToString() + dataGridView1.Rows[1].Cells[4].Value.ToString();
    dataGridView1.Rows[3].Cells[4].Value = f;

    String g;// total value for row 3 cell 5
    dataGridView1.Rows[1].Cells[5].Value.ToString();
    dataGridView1.Rows[2].Cells[5].Value.ToString();
    g = dataGridView1.Rows[1].Cells[5].Value.ToString() + dataGridView1.Rows[1].Cells[5].Value.ToString();
    dataGridView1.Rows[3].Cells[5].Value = g;

    String h;// total value for row 3 cell 6
    dataGridView1.Rows[1].Cells[6].Value.ToString();
    dataGridView1.Rows[2].Cells[6].Value.ToString();
    h = dataGridView1.Rows[1].Cells[6].Value.ToString() + dataGridView1.Rows[1].Cells[6].Value.ToString();
    dataGridView1.Rows[3].Cells[6].Value = h;

    String i;// total value for row 3 cell 7
    dataGridView1.Rows[1].Cells[7].Value.ToString();
    dataGridView1.Rows[2].Cells[7].Value.ToString();
    i = dataGridView1.Rows[1].Cells[7].Value.ToString() + dataGridView1.Rows[1].Cells[7].Value.ToString();
    dataGridView1.Rows[3].Cells[7].Value = i;

    String j;// total value for row 3 cell 8
    dataGridView1.Rows[1].Cells[8].Value.ToString();
    dataGridView1.Rows[2].Cells[8].Value.ToString();
    j = dataGridView1.Rows[1].Cells[8].Value.ToString() + dataGridView1.Rows[1].Cells[8].Value.ToString();
    dataGridView1.Rows[3].Cells[8].Value = j;


    String k;// total value for row 3 cell 9
    dataGridView1.Rows[1].Cells[9].Value.ToString();
    dataGridView1.Rows[2].Cells[9].Value.ToString();
    k = dataGridView1.Rows[1].Cells[9].Value.ToString() + dataGridView1.Rows[1].Cells[9].Value.ToString();
    dataGridView1.Rows[3].Cells[9].Value = k;

    String l;// total value for row 3 cell 10
    dataGridView1.Rows[1].Cells[10].Value.ToString();
    dataGridView1.Rows[2].Cells[10].Value.ToString();
    j = dataGridView1.Rows[1].Cells[10].Value.ToString() + dataGridView1.Rows[1].Cells[10].Value.ToString();
    dataGridView1.Rows[3].Cells[10].Value = j;

    String n;// total value for row 3 cell 11
    dataGridView1.Rows[1].Cells[11].Value.ToString();
    dataGridView1.Rows[2].Cells[11].Value.ToString();
    n = dataGridView1.Rows[1].Cells[11].Value.ToString() + dataGridView1.Rows[1].Cells[11].Value.ToString();
    dataGridView1.Rows[3].Cells[11].Value = n;
}



Graph Errpr.jpg - Google Drive[^]

What I have tried:

I had tried converting to double but still error
Posted 13-Jun-18 17:27pm
Updated 14-Jun-18 9:20am
v3
Comments
Maciej Los 14-Jun-18 2:15am
   
String n;// total value for row 3 cell 11

How string variable can hold numeric value? Use proper data type!

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

All wrong! As i mentioned in the comment to the question, you have to use proper data type!

Seems, you want to add a sum of two rows in the third one - for each column. So, your code can be simplified to:
for(int i=0; i<12; i++)
{
    dataGridView1.Rows[2].Cells[i].Value = dataGridView1.Rows[0].Cells[i].Value + dataGridView1.Rows[1].Cells[i].Value;
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 14 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100