输入字符串在C#窗体中的格式不正确 [英] input string was not in a correct format in C# windows form

查看:257
本文介绍了输入字符串在C#窗体中的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btn_Add_Job_card_Click(object sender, EventArgs e)
       {

           Validation_Add_Job_card_order();
           if (validate == true)
           {
               DataRow r = dt.NewRow();
               // = dt.Rows.Count + 1;
               r[0] = dt.Rows.Count + 1;
               //r[0] = txt_Items_No.Text;
               r[1] = txt_Job_card.Text;
               r[2] = txt_Value_Part.Text;
               r[3] = CB_Auto_parts.Text;
               r[4] = x;
               r[5] = CB_Providers_desc.Text;
               r[6] = CB_Hand_installation.Text;
               r[7] = txt_Notes.Text;

               dt.Rows.Add(r);
               dgv_Add_job_card.DataSource = dt;
               ClearText();

               txt_Total_Values.Text = (from DataGridViewRow row in dgv_Add_job_card.Rows
                                        where row.Cells[2].FormattedValue.ToString() != string.Empty
                                        select Convert.ToDouble(row.Cells[2].FormattedValue)).Sum().ToString();
           }
       }

private void btn_Add_Click(object sender, EventArgs e)
        {
 for (int i = 0; i < dgv_Add_job_card.Rows.Count - 1; i++)
                    {
                        clsParts.ADD_AUTO_PART_DETAILS(Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[0].Value)
                                                      ,dgv_Add_job_card.Rows[i].Cells[1].Value.ToString()
                                                      ,dgv_Add_job_card.Rows[i].Cells[2].Value.ToString()
                                                      ,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[3].Value)
                                                      ,Convert.ToBoolean(dgv_Add_job_card.Rows[i].Cells[4].Value)
                                                      ,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[5].Value)
                                                      ,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[6].Value).
                                                      ,dgv_Add_job_card.Rows[i].Cells[7].Value.ToString());
                    }
}

推荐答案

我们无法运行您的代码并说它就是那个位 - 因为我们可以单独运行您的代码并获得相同的结果 - 我们无法访问您的DGV或者您填写的数据。



我们必须猜测问题可能是什么,你在哪里简单地使用调试器会告诉你究竟是什么问题。

所以使用调试器。在调试器下运行你的应用程序(VS中的F5)并解决问题。

发生错误时,查看它所抱怨的代码行的部分,看看有什么价值与DGV期望的数据不匹配。然后你可以回顾你的代码来找出原因 - 我们做不到!



如果我猜,我会说它可能是 dgv_Add_job_card.Rows [i] .Cells [0] .Value 不是整数或整数的字符串表示 - 但它包含的值是什么,以及该值仅来自何处你可以告诉! :笑:
We can't run your code and say "it's that bit" - because we can;t run your code in isolation and get the same results you do - we don't have access to your DGV or top the data you are filling it with.

We'd have to guess what the problem might be, where for you a simple use of the debugger will tell you exactly what the problem is.
So use the debugger. Run your app under the debugger (F5 in VS) and make the problem happen.
When the error occurs, look at the parts of the line of your code it is complaining about, and see what value does not match the data the DGV is expecting. Then you can look back in your code to work out why - we can't!

If I was to guess, I'd say it's probably that dgv_Add_job_card.Rows[i].Cells[0].Value isn't an integer or the string representation of an integer - but what value it contains and where the heck that value comes from only you can tell! :laugh:


你调用一些 Convert.To * 的方法,不能保证作为参数传递的字符串真的可以解析为所需类型的值。如果没有,则抛出异常,您可以处理并恢复。但这不是正确的方法。即使是类名转换也有些误导。你需要了解到底发生了什么,这只不过是解析。对于每种预期类型,最好使用 TryParse 方法。例如,对于 int ,您可以使用 bool int.TryParse(string,out int),依此类推。如果此方法返回false,则表示输入字符串不是预期的格式,并且不通过第二个 out 参数返回有效值。



此外,您的代码示例强烈建议您倾向于使用表示数据的字符串,而不是数据本身。您应该更好地避免它,保留字符串主要用于输出目的,例如在屏幕上显示。例如,对于类型 decimal 和整数类型,您可以使用控件 System.Windows.Forms.NumericUpDown ( in也可以间接用于浮点类型,如果你需要具有固定精度的输入,通常就是这种情况): NumericUpDown类(System.Windows.Forms) [ ^ ]。



此外,您可以使用 System.Windows.Forms.DataGridView ,您可以在其中使用强类型单元格数据。某些类型的派生自 System.Windows.Forms.DataGridViewCell 的单元格已经可用于某些常用数据类型,有些可以自行开发。请参阅:

DataGridView类(System.Windows.Forms) [ ^ ],

DataGridViewCell类(System.Windows.Forms) [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value(v=vs.110).aspx> DataGridViewCell.Value属性(System.Windows.Forms) ) [ ^ ],

DataGridViewCell.ValueType属性(System.Windows.Forms) [ ^ ]。



等......这只是一些想法,需要考虑的事情。



-SA
You call a number of Convert.To* methods having no guarantee that the string passed as an argument really can be parsed as a value of required type. If it doesn't, you throw an exception, which you can handle and recover. But this is not a right way. Even the class name Convert is somewhat misleading. You need to understand what really happens, and this is nothing but parsing. You should better use the TryParse method for each expected type. For example, for int, you can use bool int.TryParse(string, out int), and so on. If such method returns false, you know that the input string is not in an expected format, and the valid value is not returned via the second out parameter.

Moreover, your code sample strongly suggests that you tend to work with strings representing data, instead of data itself. You should better avoid it, reserving strings mostly for output purposes, such as showing on screen. For example, for the type decimal and integer types, you can use the control System.Windows.Forms.NumericUpDown (in can also be used indirectly for floating-point type, if you need input with fixed precision, which is usually the case): NumericUpDown Class (System.Windows.Forms)[^].

Also, you can use System.Windows.Forms.DataGridView where you can use strongly-typed cell data. Some types of the cell derived from System.Windows.Forms.DataGridViewCell are already available for some commonly used data types, some you can develop yourself. Please see:
DataGridView Class (System.Windows.Forms)[^],
DataGridViewCell Class (System.Windows.Forms)[^],
DataGridViewCell.Value Property (System.Windows.Forms)[^],
DataGridViewCell.ValueType Property (System.Windows.Forms)[^].

And so on… It were just some of the idea, something to think about.

—SA


这篇关于输入字符串在C#窗体中的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆