Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently using GemBox in a C# winform to send a DGV's data to excel. The data is being passed but the numbers are being passed as strings.

What I have tried:

-I tried using number formatting based on the GemBox documentation but it doesn't seem to do anything; it's probably because it detects a string in excel. Here's an example:
C#
worksheet.Cells["B" + current_line_number].Style.NumberFormat = NumberFormatBuilder.Number(2, useThousandsSeparator: true);

worksheet.Cells["B" + current_line_number].Value = data_holder.Rows[i].Cells[0].Value;    //Rotor Speed

-I tried casting the dgv rows like this:
C#
for (int i = 0; i <= data_holder.Rows.Count - 2; i++)
     {

          //Adding Data
          worksheet.Cells["B" + current_line_number].Value = (double)data_holder.Rows[i].Cells[0].Value;
                
          //Moving to next line
          current_line_number++;

     }


and like this with and without .GetType:
for (int i = 0; i <= data_holder.Rows.Count - 2; i++)
     {

          //Adding Data
          worksheet.Cells["B" + current_line_number].Value = data_holder.Rows[i].Cells[0].Value.ToDouble;
                
          //Moving to next line
          current_line_number++;

     }

-In sort of a desperate attempt I also created a separate double, assigned the dgv row to it and then assigned the double to the cell.

-I also messed around with the DGV seeting and set the cell format to N4 but once again no change

Thanks!
Posted
Updated 21-Dec-22 3:48am
v2

You can't cast a string to a double, even if the string contains a value like "1234.56". Casting is only allowed when the class of the object being casted and the "destination" class are related, or when an explicit cast operator for that specific pair of classes has been created (which creates a new object of the destination class with the info from the source - changes to the destination are not reflected back in the source).
For example, you can cast from an int to a double and vice versa, or from a Form to a Control because the former derives from the latter - but you may get an error trying to cast in the other direction as the Button class for example is not directly related to a Form

What you have to do is convert the data using the double.TryParse method[^]:
C#
string str = "123.45";
double d;
if (!double.TryParse(str, out d))
   {
   ... report problem or log it ...
   return;
   }
Console.WriteLine(d);
You could use the double.Parse method[^] but if the source is not a valid double value then an exception will be thrown, potentially crashing your app.
 
Share this answer
 
Comments
Blackth_rn 21-Dec-22 9:51am    
I just tried converting it there instead of casting and it worked. Thank you for explaining it!
OriginalGriff 21-Dec-22 10:21am    
You're welcome!
On the C# side, you must be using the TextBox Column Type for the DGV column: if you want Excel Cells to hold doubles, you need to convert to Double.

In Excel VBA see: https://www.exceldemy.com/excel-vba-convert-string-to-double/

fyi: for C#, study the Double.Parse() and Double.TryParse() methods.

Even the free (no support) version of GemBox has excellent examples.
 
Share this answer
 
v2

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