我是编程和C#的新手。我正在尝试为datagridview中的每一行添加某些列的值。 datagridview未链接到任何数据库,其值来自用户输入但失败。任何帮助将不胜感激。
这是我的代码:
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());
}
}
答案 0 :(得分:1)
您不需要内循环,删除它并获得解决方案
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());
}