Reputation: 71
I am fairly new to programming and C#. I am trying to add the values of certain columns for each row in my datagridview. The datagridview is not linked to any database, its value comes from the user input but have failed. Any help would be appreciated.
Here is my code:
double a,b,c,d,total = 0;
foreach(DataGridViewRow row in dataGridView1.Rows)
{
foreach(DataGridViewColumn col in dataGridView1.Columns)
{
a = Convert.ToDouble(row.Cells[3].Value);
b = Convert.ToDouble(row.Cells[4].Value);
c = Convert.ToDouble(row.Cells[5].Value);
d = Convert.ToDouble(row.Cells[6].Value);
total = a + b + c + d;
MessageBox.Show(total.ToString());
}
}
Upvotes: 0
Views: 176
Reputation: 2885
You dont need inner loop, remove it and you get solution
double a,b,c,d,total = 0;
foreach(DataGridViewRow row in dataGridView1.Rows)
{
a = Convert.ToDouble(row.Cells[3].Value);
b = Convert.ToDouble(row.Cells[4].Value);
c = Convert.ToDouble(row.Cells[5].Value);
d = Convert.ToDouble(row.Cells[6].Value);
total = a + b + c + d;
MessageBox.Show(total.ToString());
}
Upvotes: 1