Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

XML
 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.
Posted
Updated 1-Oct-15 19:47pm
v3
Comments
Maciej Los 2-Oct-15 2:02am    
Debug the programme to find out...
DamithSL 2-Oct-15 2:04am    
have you debug and check the failing values?

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
C#
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..
   }
 
}
 
Share this answer
 
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.
 
Share this answer
 
v2
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:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900