0

I'm trying to represent the right double value in my Listbox. The values in my SQL Database are represented as "0,09" for example. Now I put these values which are in a certain column into a Listbox with a "SELECT DISTINCT" statement. When searching in the database I need to write "0.09" since "0,09" to find the value. That's not the problem. I tried to convert every single double value with

 CultureInfo culInfo = new CultureInfo("en-US");
 for (int i = 0; i < convertCol.Count; i++)
            {
                storeDouble = convertCol[i];
                convertToString = storeDouble.ToString("0.######");


                if (convertToString.Contains(","))
                {
                    convertedString = convertToString.Replace(",", ".");
                    convertBackToDouble = double.Parse(convertedString, culInfo);
                        //Convert.ToDouble(convertedString);
                    convertedCol.Add(convertBackToDouble);

                }

                else 
                {
                    convertedCol.Add(convertCol[i]);

                }
            }

            DataColumn dc = new DataColumn(form1.getColName());
            dc.DataType = typeof(double);
            datTable.Columns.Add(dc);

            for (int j = 0; j < convertedCol.Count; j++)
            {
                datTable.Rows.Add(convertedCol[j]);
            }

            form1.colStorList.DisplayMember = form1.getColName();
            form1.colStorList.ValueMember = "Column";
            form1.colStorList.DataSource = datTable;

When I leave the cultureInfo out and try "Convert.ToDouble" then "0,09" gets converted to "9.0". With the cultureInfo it seems right but my Listbox has still entries like "0,09". How to "force" the ListBox to display my values as "0.09" ?

MKX2015
  • 155
  • 1
  • 2
  • 13
  • I am not sure why this happening. But if it always 2 decimals then `value/100` might work. – Mahesh Feb 26 '15 at 08:16
  • "The values in my SQL Database are represented as "0,09" for example" - why don't you represent them as *numbers* in your database? When you pick the correct types for database fields, variables etc, a lot of problems like this just disappear... – Jon Skeet Feb 26 '15 at 08:17
  • No it can be more than two decimals - as numbers? Integers? It should be double values – MKX2015 Feb 26 '15 at 08:18

1 Answers1

1

try add this code

form1.colStorList.FormatInfo = new CultureInfo("en-US");

after

form1.colStorList.ValueMember = "Column";
ASh
  • 34,632
  • 9
  • 60
  • 82
  • Wow - That solved the problem. Thank you very much! But he still doesn't find the values in my database so there has to be something else. Thanks! – MKX2015 Feb 26 '15 at 09:03