[英]I got this exception: System.FormatException: 'Input string was not in a correct format.'
I want to make a management program with an SQL database attached.我想制作一个附加了 SQL 数据库的管理程序。 My problem is that I want to gather all the values from one column to a textbox.我的问题是我想将一列中的所有值收集到一个文本框中。 That column [7] from the datagridview has values based on the calculation of 2 other columns [4]and [5] in the same datagridview formated as money. datagridview 中的该列 [7] 的值基于对同一 datagridview 中其他 2 个列 [4] 和 [5] 的计算,这些列被格式化为货币。
I tried to convert it to string or int without success.我试图将其转换为字符串或整数,但没有成功。 Please Help!请帮忙!
public void gridTotal()
{
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value);
}
facturaTotal.Text = sum.ToString("C");
}
dataGridView1.Rows[i].Cells[7].Value
may be null
or empty String
or something else, which cannot be converted into double.可能是null
或空String
或其他东西,不能转换成双精度。 Instead I would recommend相反,我会推荐
double val = 0;
if (double.TryParse(dataGridView1.Rows[i].Cells[7].Value, out val)) sum += val;
If the cells contain real numbers written with a dot, like 2.3
, this error will occur.如果单元格包含用点写的实数,例如2.3
,则会发生此错误。 It is common mistake, you should use CultureInfo
to avoid this problem.这是常见的错误,你应该使用CultureInfo
来避免这个问题。
sum += Double.Parse(dataGridView1.Rows[i].Cells[7].Value, CultureInfo.InvariantCulture);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.