简体   繁体   English

我得到了这个异常:System.FormatException:'输入字符串的格式不正确。'

[英]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.

相关问题 c# 如何修复:“未处理的异常:System.FormatException:输入字符串的格式不正确。” - c# How Can I Fix: “Unhandled Exception: System.FormatException: Input string was not in a correct format.” System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# System.FormatException: &#39;输入字符串的格式不正确。&#39; - System.FormatException: 'Input string was not in a correct format.' System.FormatException: &#39;输入字符串的格式不正确。&#39; 数据网格 - System.FormatException: 'Input string was not in a correct format.' data grid System.FormatException:&#39;输入字符串的格式不正确。 - System.FormatException: 'The input string does not have the correct format.' System.FormatException: '输入字符串的格式不正确。' WinForms - System.FormatException: 'Input string was not in a correct format.' WinForms Linq 错误输入字符串的格式不正确。 System.Exception {System.FormatException} - Linq Error Input string was not in a correct format. System.Exception {System.FormatException} C#当我使用TryParse时,为什么会收到“未处理的异常:System.FormatException:输入字符串的格式不正确。” - C# Why I get “Unhandled Exception: System.FormatException: Input string was not in a correct format.” when I use TryParse? 异常详细信息:System.FormatException:输入字符串的格式不正确。 解析不起作用 - Exception Details: System.FormatException: Input string was not in a correct format. Parse not working Int32.Parse(“356882”) 生成异常 System.FormatException: '输入字符串的格式不正确。' - Int32.Parse(“356882”) generate exception System.FormatException: 'Input string was not in a correct format.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM