I am writing a program in c# and have encountered an annoying little problem. I need to convert fields from dataGridView control to double in order to store them in database. However, it doesn't convert correctly.
The original values in dataGridView use comma (,) for decimal point separation.
Here's the original code:
double number = Convert.ToDouble(dataGridView1.Rows[row].Cells[1].Value)
The cell is textbox type. By using this method 12,3 becomes 12,3E+14
For some unknown reasons, the converter multiplies the number by 10 with the power 14.
After reading some questions, I stumbled upon another possible solution
double number = Convert.ToDouble(dataGridView1.Rows[row].Cells[1].Value, CultureInfo.CurrentCulture);
Still the same result. 12,3 -> 12,3E+14
double number = Convert.ToDouble(dataGridView1.Rows[row].Cells[1].Value.ToString(), CultureInfo.CurrentCulture);
This gets even more interesting. Instead of 12,3 i'm getting 123
Am I missing something? Is there a simple solution to all of this?
EDIT: just in case - the database field type is set to real and dataGridView cell is TextBox type, so the input is string type.
EDIT2: The thing I am trying to do is to load value (real) from database to DGV, save it to a different database table as real, then load the value from the newly written database table (real) and show it in another DGV. After writing the values from DGV1 to database, their type is Single. After writing them to database the 2nd time, however, the value becomes String type, even though i did no changes to it. Could this be what's distorting the conversion values? I mean the 1st time they show up as 12,3E+14 instead of 12,3. I could just divide them by Math.Pow(10, 14) to get the original value, but this is dirty practice :/
FINAL EDIT: Thank you all for your help, but the real problem in this case was my carelessness. As this program is being developed for quite a while, back when I was still getting used to c# syntax, I have included some variable type mix-ups, which has led me to having to review all the types in the code. Again, thank you all for help.
TL;DR - keep your variable types consistent.