Click here to Skip to main content
16,016,477 members
Please Sign up or sign in to vote.
1.00/5 (1 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


C#
   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
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

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:
C#
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;
}
 
Share this answer
 

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