0

I have written an extension method to convert the Datatable to a generic observable collection as below:

public static ObservableCollection<T> ToObservableCollection<T>(this DataTable dt)
        {

            var columnNames = dt.Columns.Cast<DataColumn>()
                .Select(c => c.ColumnName.ToUpperInvariant())
                .ToList();

            var properties = typeof(T).GetProperties();

            List<T> returnList = dt.AsEnumerable().Select(row =>
            {
                var objT = Activator.CreateInstance<T>();

                foreach (var pro in properties)
                {
                    if (columnNames.Contains(pro.Name.ToUpperInvariant()))
                    {
                        if (row[pro.Name] != DBNull.Value && Convert.ToString(row[pro.Name]) != string.Empty)
                        {
                            if ((pro.PropertyType == Type.GetType("System.Boolean")) || (row[pro.Name].GetType() == Type.GetType("System.Boolean")))
                                pro.SetValue(objT, Convert.ToBoolean(row[pro.Name]), null);

                            else if (row[pro.Name].GetType() == Type.GetType("System.Int64"))
                                pro.SetValue(objT, Convert.ToInt32(row[pro.Name]), null);
                            else
                                pro.SetValue(objT, row[pro.Name], System.Reflection.BindingFlags.Default, null, null, CultureInfo.CurrentUICulture);
                        }
                    }
                }

                return objT;
            }).ToList();

I have set the decimal symbol to Comma(,) in my machine(image) and the Number Format in the CurrentCulture of my application is having the same(image).

The data table returned from the database also has the decimal values where the decimal separator is comma(,) see image

When i convert this data table to observable collection using the method above i get the Observable collection which has the decimal value but instead of having the decimal separator comma(,) i see period(.) as a decimal separator.

What am i missing here?

Please let me know if my question is not clear. I'll add more details accordingly.

I tried to reproduce this behavior using the sample WPF application and i am getting comma(,) as the decimal separator when i do ToString() on the property and then display it in a label. if i don't do ToString() then period(.) is being used as a decimal separator(image)

Tried the solution provided in Rick Strahl's Web Log but it didn't help as well.

4
  • In which display do you look at the decimal values? Jun 19, 2018 at 19:20
  • @GertArnold In WPF application (in Label bound to a property).
    – Mallikh
    Jun 19, 2018 at 19:24
  • Well, apparently it's got a different culture. The display format is in no way an intrinsic attribute of a decimal value, the display culture rules. Jun 19, 2018 at 19:41
  • @GertArnold I tried to reproduce using a sample application. when i use ToString() on the property then comma(,) is used as the decimal separator else period(.) is used.
    – Mallikh
    Jun 19, 2018 at 20:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.