I'm having issues getting the right visualization for some datas displayed in DataGrid (WPF, not Forms DataGridView).
I'm loading my datatable which contains some numeric columns. Some values have a decimal part, some don't, like 2 , 2.5, 10 and so on.
What I've seen is DataGrid automatically displays decimals positions only if they're necessary, so "5" is displayed just like "5". For readability purpose, I'd like to format all numbers with one decimal position. I tried to use Format(myNumber, "0.0")
, but this return a string: the problem then is the ordering, which is not executed as expected (this way it's ordered like string, not numbers).
Any ideas? Does someone already faced this?
I finally managed to solve this.
The problem is that I have to generate the columns in code. My DataGrid is set with AutoGenerateColumns=True.
Then I added this code:
Private Sub DataGrid_OnAutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs) Handles DataGrid.AutoGeneratingColumn
If e.PropertyType = GetType(Date) Then
TryCast(e.Column, DataGridTextColumn).Binding.StringFormat = "yyyy/MM/dd HH:mm:ss"
End If
End Sub
In this way, I can easily adjust the date format to user's preferences.