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.