嗨 我的C#应用程序中有一个datagridview,它绑定到一组用户定义的对象.我对象中的某些数据项是数字数据类型-特定于float和uint.数据gridview已填充数据,我正在进行单元级数据验证. 验证期间,任何时候我尝试使用Convert.ToSingle()或Convert.ToUInt16()转换数据,并且如果输入单元格值为"0",则会出现输入字符串格式不正确"的异常.但是,如果我的单元格值为"00"或任何其他非零数值,则转换函数(Convert.ToSingle()或Convert.ToUInt16())成功. 我不明白为什么Convert.ToSingle()或Convert.ToUInt16()的"0"输入值会引发格式异常.知道我应该怎么做才能防止这种异常吗?感谢您的帮助! 谢谢 K
Hi I have a datagridview in my C# application which is bound to an array of user defined objects. Some of the data items in my objects are numeric data type - float and uint to be specific. The data gridview has been populated with data and I am doing the cell level data validation. During validation, anytime I try to convert data by using Convert.ToSingle() or Convert.ToUInt16(), and if the input cell value is "0", I get a "Input String was not in a correct format" exception. But if my cell value is "00" or any other non-zero numeric value, the convert functions (Convert.ToSingle() or Convert.ToUInt16()) are successful. I do not understand why "0" input value for Convert.ToSingle() or Convert.ToUInt16() will throw a format exception. Any idea what I should do to prevent this exception? I appreciate any help! Thanks K
推荐答案我怀疑是您的其他代码有问题-我在下面的代码中尝试了此操作,没有问题.检查您实际上是否在访问正确的单元格,并在必要时发布代码片段.可能值得使用Singe.TryParse而不是Convert.ToSingle,因为它是通过bool报告问题的,而不是通过用户输入来报告问题. I suspect that it''s your other code that is the problem - I tried it here with the code below and had no problem. Check you are actually accessing the right cell, and post a code fragment if necessary. It may be worth your using Singe.TryParse rather than Convert.ToSingle, as that reports problems via a bool, rather than excepting on user input. String s = "0"; Single si = Convert.ToSingle(s); if (Single.TryParse(s, out si)) { ... }
您好 感谢您的答复.我尝试了您发布的代码,但在Convert.ToSingle(s)中仍然存在异常.然后,当我将硬编码字符串设置为"0"时,我对Convert.ToSingle(s)进行了注释,TryParse()成功了. 但是TryParse()在datagridview输入为"0"时不成功.这是一段代码,它给了我这个问题: Hi Thank you for your response. I tried the piece of code you posted, I still have the exception raised in Convert.ToSingle(s). Then I commented Convert.ToSingle(s) and the TryParse() was successful, when I had the hardcoded string as "0". But the TryParse() is not successful with the datagridview input as "0". Here is the piece of code, which is giving me the issue: private void LKDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs CellEvent) { Single TempFloatVal = 0.0f; switch ((ELKCol) CellEvent.ColumnIndex) { case ELKCol.Delta: if (Single.TryParse(CellEvent.FormattedValue.ToString(), out TempFloatVal)) MessageBox.Show("Value of Delta: " + TempFloatVal.ToString()); else MessageBox.Show("Input value: " + CellEvent.FormattedValue.ToString()); break; default: break } }
"Delta"的数据类型为float.每当我在datgridview中为Delta输入非零数值时,Single.TryParse()都会成功.每当我在datgridview中为Delta输入0值时,Single.TryParse()都会失败. 我只是无法弄清楚发生了什么.任何建议都非常感谢! 谢谢 K
The data type for "Delta" is float. Any time i have non-zero numeric value entered for Delta in datgridview, Single.TryParse() succeeds. Any time I enter a 0 value for Delta in datgridview, Single.TryParse() fails. I just am not able to figure out what is going on. Any suggestions is very much appreciated! Thanks K
更多推荐
C#DataGridView和数值数据
发布评论