I have spent so much time trying to figure out how numbers work in different cultures in C#. I have sorted all the scenarios except when adding a number to datatable. In db I have number in US format (e.g. 1.32), in frontend if the culture is german then it show 1,32 in grid.
I can convert 1.32 to 1,32 using
Convert.ToDouble("1.32").ToString(Culture.GetCultureInfo("de-DE"))
but when I add this into a data table in a column of type double it becomes 132.0.
the number in db is of type double. This is how I try to convert it into proper culture format:
dt.Rows.Add(new Object[] { Convert.ToDecimal(ds.Tables[0].Rows[i][5]).ToString(culture.NumberFormat) })
One way would be to format the number in rowdatabound event of grid but is this a proper solution? There should be a way to store culture specific number in datatable.
When you save the value to the database/datatable it should be of type double not string. A double value does not care about formatting, only when it is converted (and displayed) as a string.
As for formatting goes, the GridView uses the culture specified in CultureInfo.CurrentCulture (can be changed from code). But you can also set you specific formatting on BoundField using property DataFormatString