How to prevent columns not showing up in WPF datagrids when using '/' in the header?

I have a WPF Datagrid in my application where I set the ItemSource value in DefaultView for the DataTable that I created. The problem is that whenever I set the column column of one of the columns in my DataTable to a row containing "/", the header is displayed in the DataGrid control, but there is no value. How can I get around this?

enter image description here

This is the same table where '/' is replaced by '-' enter image description here

as an aside, this also happens with headers that have. in them. Thus, any decimal number will lead to the same behavior.

My grid is defined as

<DataGrid x:Name="dgLFKPI" /> 

and I set the value in the code behind (yes, it should be in view mode and MVVM, but it is an outdated application that moves slowly this way).

 dgLFKPI.ItemsSource = dt.DefaultView; 
+6
source share
2 answers

Special characters in column names are not parsed correctly by the binding path parser.

So, binding a column to 3/4 is actually only a binding to property 3 , not to property 3/4 . (Same thing with binding . )

You will probably see binding errors in the debug window at runtime, which should say the same thing.

System.Windows.Data error: 40: BindingExpression path error: Property '3' not found in 'object' '' DataRowView '

In accordance with this answer

There are several different characters that have special meaning in the binding path, including full stop ('.'), Slash ('/'), square brackets ('[', ']') and brackets ('(', ')' ), brackets will cause the application to crash. These special characters can be escaped by the surrounding anchor path with square brackets. More information about paths and character escaping can be found in [Binding Declaration Overview] [2]

This related question also contains a good solution for working with meshes that want to use automatically generated columns.

Use the AutoGeneratingColumn event and manually create bindings for columns with these special characters in their name to avoid them using square brackets.

 <DataGrid x:Name="dgLFKPI" AutoGeneratingColumn="dgLFKPI_AutoGeneratingColumn" /> 
 private void dgLFKPI_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyName.Contains('/') && e.Column is DataGridBoundColumn) { var col = e.Column as DataGridBoundColumn; col.Binding = new Binding(string.Format("[{0}]", e.PropertyName)); } } 
+4
source

You need to set the columns specifically, and then set the Header value for the column to its share value.

 <DataGrid x:Name="dgLFKPI"> <dg:DataGridTextColumn Header="0" Width="50" Binding="{Binding <columnname>}" /> <dg:DataGridTextColumn Header="1/4" Width="50" Binding="{Binding <columnname>}" /> ... </DataGrid> 

Alternatively, you can create it in code:

 var col = new DataGridTextColumn{ Header = "1/4"; Binding = new Binding("<columnname>")}; dataGrid1.Columns.Add(col); 
+1
source

Source: https://habr.com/ru/post/973037/


All Articles