This question already has an answer here:
I have a WPF Datagrid in my application where I set the value of the ItemSource to the DefaultView of a DataTable that I have constructed. The problem lies in the fact that whenever I set the ColumnName of one of the columns in my DataTable to a string that includes '/' the header shows up in the DataGrid control but the values do not. How can I get around this?
This is the same table with the '/' replaced with '-'
as an aside this also appears to happen with headers that have a '.' in them. So any decimal will cause the same behavior.
My grid is defined as
<DataGrid x:Name="dgLFKPI" />
and I am setting the value in code behind (yes it should be in a viewmodel and MVVM but it is a legacy app that is slowly moving that way).
dgLFKPI.ItemsSource = dt.DefaultView;
You need to set the columns specifically, then set the
Header
value of the column to your fraction value.Alternatively, you could build it up in the codebehind:
The special characters in the column names are incorrectly parsed by the binding path parser.
So a column binding to
3/4
is actually only binding to the property3
, and not the property3/4
. (The same thing with the.
binding)You'll probably see binding errors in the debug window while this is running that should say the same sort of thing.
According to this answer
That linked question also contains a good solution for dealing with grids that want to use auto-generated columns.
Use the
AutoGeneratingColumn
event, and manually create the bindings for columns with these special characters in their name to escape them using the square brackets.