将字符串数组(从datagridview读取)转换为double数组 [英] converting string array(read from datagridview) into double array

查看:149
本文介绍了将字符串数组(从datagridview读取)转换为double数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.net 4.0中制作Windows应用程序。我正在通过datagridview列中的数据填充字符串数组。现在我想将该字符串数组转换为双数组。



i使用以下代码:



< pre lang =xml> double [] column0Array = new double [dataGridView1.Rows.Count];

string [] column0Array1 = new string [dataGridView1.Rows.Count];


< pre lang = cs > foreach(dataGridView1.Rows中的DataGridViewRow行)
{
column0Array1 [i] = row.Cells [Sectional Area ] .Value!= null? row.Cells [Sectional Area]。Value.ToString():string.Empty;

column0Array [i] = Double.Parse(column0Array1 [i]);



// column0Array [i] =(Convert.ToDouble(column0Array1 [i]));
textBox6.Text = textBox6.Text + column0Array [i];
i ++;
}
< / pre >





但它会在线上触发错误

column0Array [i] = Double .Parse(column0Array1 [I]);错误是输入字符串的格式不正确。



我也尝试使用column0Array [i] =(Convert.ToDouble( column0Array1 [I]));但它会发出相同的错误。



请帮助,提前谢谢。

解决方案

它可能很多原因,你最好调试你的代码并检查下降的值并尝试找出这些值无法转换的原因。
例如你的输入字符串可能有额外的空格或空值或其他一些字符,千分隔符等...

如果您不确定输入是否为数字,则可以使用Double.TryParse方法将字符串解析为double。如果解析失败,它不会给出异常。例如

  string  inputstring = column0Array1 [i] .Trim(); 
double result = 0 ;
if (!string.IsNullOrEmpty(inputstring))
{
double value ;
if Double .TryParse(inputstring, out value ))
{
result = value ;
} 其他
{
// 无法将输入转换为双..
}

}


最常见的原因输入字符串格式不正确错误是:

1.字符串为空

2.字符串包含字符或坏十进制/千位分隔符(取决于本地化/区域设置)。



我建议使用 Double.TryParse [ ^ ]提供了使用指定的样式将字符串转换为双重表示的功能,特定文化格式。



BTW,为什么要使用double数组?使用列表< double> [ ^ ] !此对象提供搜索,排序和聚合数据的方法。


查看错误消息:输入字符串的格式不正确。

这意味着问题在于您的数据,而不是您的代码。您的一个或多个表值不是基于字符串的数字。它可能是null,它可能是字母:我们无法分辨。

所以使用double.TryParse而不是double.Parse和Convert.ToDouble,并检查哪些值失败。然后,您可以回顾一下您的数据并找出原因,并在源头更正。



可能您正在从数据库中读取数据,并且有人存储了数据NVARCHAR列中的值(这是一种不好的做法,因为它会导致这样的问题)并且不会为验证输入而烦恼。当你找到这个白痴的时候,打他的头,直到他同意再也没有拿过来修理它! :笑:

i am making windows application in .net 4.0. I am filling string array by data which is from column of datagridview. Now I want to convert that string array into double array.

i am using following code :

 double[] column0Array = new double[dataGridView1.Rows.Count];

  string[] column0Array1 = new string[dataGridView1.Rows.Count];


<pre lang="cs">foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               column0Array1[i] = row.Cells["Sectional Area"].Value != null ? row.Cells["Sectional Area"].Value.ToString() : string.Empty;

               column0Array[i] = Double.Parse(column0Array1[i]);



               //column0Array[i] = (Convert.ToDouble(column0Array1[i]));
               textBox6.Text = textBox6.Text + column0Array[i];
               i++;
           }
</pre>



but it fires error on line
column0Array[i] = Double.Parse(column0Array1[i]); error is "Input string was not in a correct format."

I also try by column0Array[i] = (Convert.ToDouble(column0Array1[i])); but it fires same error.

help please, thanks in advance.

解决方案

it could be many reasons, you better debug your code and check the values which falling and try to find why those values failed to convert.
for example your input strings may have extra spaces or empty values or some other characters, thousand separators etc...
if you not sure the input is number or not you can use Double.TryParse method to parse the string as double. it will not give exception in case of failed to parse. for example

string inputstring = column0Array1[i].Trim();
double result=0;
if (!string.IsNullOrEmpty(inputstring))
{   
   double value;
   if (Double.TryParse(inputstring, out value)) 
   {    
       result= value;
   }else
   {
       // failed to convert input to a double..
   }
 
}


A most common reason of Input string was not in a correct format error is:
1. string is null
2. string contains characters or bad decimal/thousand separator (depends on localization/regional settings).

I'd suggest to use Double.TryParse[^] which provides functionality to convert string into its double representation using the specified style and culture-specific format.

BTW, why to use array of double? Use List<double>[^] instead! This object provides methods to search, sort and aggregate data.


Look at the error message: "Input string was not in a correct format."
What that means is that the problem is with your data, not your code. One or more of your table values is not a string-based number. It may be null, it may be Alphabetic: we can't tell.
So use double.TryParse instead of double.Parse and Convert.ToDouble, and examine which values fail. You can then look back at your data and find out why, and correct it at source.

Probably, you are reading this from a database, and somebody has stored numeric values in a NVARCHAR column (which is a bad practice because it leads to problems like this) and not bothered to verify the inputs. When you find the idiot, smack him round the head until he agrees never took do it again and fixes it! :laugh:


这篇关于将字符串数组(从datagridview读取)转换为double数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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