Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am trying to transport datagridview data to jagged array for some reason but I couldn't succeed

the error is:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: The input line was not in the correct format.

my datagridview data is like;
|0.35|1.58|0.82|1.78|0.85|1.55|1|0|0|}


What I have tried:

C#
string linex= "";
string[] correction= null;
for (int i=0;i<= rowcount-1;i++)
{
                
    for (int j=0;j<= colmcount-1;j++)
    {
        linex= linex.ToString()+" " + dataGridView1.Rows[i].Cells[j].Value.ToString();
    }
    correction= linex.Split(',');
    rowdataexample[i] = new double[columncnt];
    for (int k = 0; k <= colmcount-1; k++)
    {
        rowdataexample[i][k] = double.Parse(correction[k]);

    }
    correction= null;
    linex= "";
}
Posted
Updated 10-Apr-17 18:40pm
v4
Comments
[no name] 10-Apr-17 10:17am    
Obviously whatever correction[k] is can't be converted to a double. Which makes total sense when you are splitting your string by comma and using a space character as your delimiter. And you should be using TryParse anyway.
ser_khan 10-Apr-17 15:39pm    
nope, it doesn't work, i tried it before.
[no name] 10-Apr-17 16:24pm    
Well "it doesn't work" only means something to you. WHAT doesn't work? What did you see when you debugged your code?
ser_khan 10-Apr-17 16:43pm    
my data became just like this correction[0]==>0.35 1.58 0.82 1.78 0.85 1.55 1 0 0
[no name] 10-Apr-17 17:12pm    
Yes ... AND? You are splitting your string on a comma. Where is the COMMA in that string?

1 solution

add comma to this
linex= linex.ToString()+"," + dataGridView1.Rows[i].Cells[j].Value.ToString();

Note:
use Double.TryParse Method (System)[^] for converting string to double
use String.Trim Method (System)[^] to avoid unwanted space in the beginning and end of the string.
 
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