int sum=0;
for ( int i=0; i < dataGridView1.Rows.Count; ++i)
{
sum += int.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
}
total.Text = sum.ToString();Value)
//showing error Input string was not in a correct format.
我也试过sum += int.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
但仍显示错误。
我甚至尝试过双重但仍然出错
请帮忙
答案 0 :(得分:0)
问题很可能是你试图解析标题中的十进制数,但是你正在使用int.Parse函数。 int.Parse会失败,如果你给它一个&#39; 5.25&#39;您得到完全相同的错误消息的值:&#34;输入字符串的格式不正确&#34;。
您可以尝试使用decimal.Parse方法解析您的号码
int sum=0;
for ( int i=0; i < dataGridView1.Rows.Count; ++i)
{
//this decimal cast to integer will ignore any numbers after the decimal point.
//e.g.: 0.6 will add 0 (absolutely nothing) to the sum variable.
//I would recommend using the other solution below.
sum += (int)decimal.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString(), System.Globalization.NumberStyles.Any);
}
total.Text = sum.ToString();
或将整数和一起更改为十进制类型
decimal sum = 0m;
for ( int i=0; i < dataGridView1.Rows.Count; ++i)
{
sum += decimal.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString(), System.Globalization.NumberStyles.Any);
}
total.Text = sum.ToString();