Cannot associate WPF DataGrid with table with columns named with spaces and other special characters

When I have a simple DataSet that has a table with simple column names (i.e. no spaces or periods), code like the following works just fine:

  DataGrid resultsGrid=...; // Actually defined in the XAML DataSet ds=...; // The is the DataSet that contains one table Binding binding = new Binding(); resultsGrid.DataContext=ds.Tables[0]; resultsGrid.SetBinding(DataGrid.ItemsSourceProperty, binding); 

In the above example, the DataGrid has the AutoGenerateColumns attribute set to True and correctly fills its data from the table in the DataSet .

However, if there are columns in my table with names containing spaces / periods or other special characters, auto-linking seems to fail. I get errors like:

System.Windows.Data Information: 20: BindingExpression cannot retrieve a value due to missing information. BindingExpression: Path = My Col. Name; DataItem = 'DataRowView' (HashCode = 8146871); target element "TextBlock" (Name = ''); target is "Text" (type "String")

Obviously, the automatically generated binding expression Path=My Col. Name Path=My Col. Name not valid. The entire path must be "cited" to allow spaces and periods. Is there a way to stick with AutoGeneratedColumns for tables with more complex column names, or do I need all the manual guidance now?

+4
source share
2 answers

You can try to process the resultsGrid.AutoGeneratingColumns - or, more preferably, do it just before the grid before the set gets the data binding - to replace or remove spaces or periods in the column names. You would have to do this with both the grid and the DataSet, so if your data is edited and saved, this method will probably be a lot more complicated than it costs. The effectiveness and feasibility of this largely depends on the structure of your application and behavior, plus I have not tested it, so take it with a significant amount of salt.

As far as I have found (which admittedly is not a very extensive domain), you are pretty much obsessed with this behavior. I suggest sending a bug to Microsoft; this seems to be the desired behavior for AutoGenerateColumns .

+3
source

You can format SQL selections for no spaces

  select [Column A] as 'Column_A' from table 
0
source

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


All Articles