问题
I'm very new to WPF. I'm having a very weird issue to just make a table, here is the code I have:
DataTable table = new DataTable();
table.Columns.Add("request/sec", typeof(string));
DataRow newRow = dataTable.NewRow();
newRow["request/sec", "na");
but it only shows a column name with "request/sec" and an empty row
however, if I do:
DataTable table = new DataTable();
table.Columns.Add("test(request/sec)", typeof(string));
DataRow newRow = dataTable.NewRow();
newRow["test(request/sec)", "na");
then it's working, it shows the column name correctly, and a row with value "na"
I have no idea why this is happening, I know it sounds ridiculously. Can anyone help?
note: I also tried "//", "/", "\/", @ I can not hardcoded the column names, it's dynamic generated by certain logic, I used the above code as examples
回答1:
Data don't show up when bind ItemsSource of a WPF DataGrid to a DataTable which contains columns like '01/14/10' - MS WPF forum discusses this problem.
In brief, this was claimed to be a WPF Toolkit bug as of March 22, 2010 and refers to tickets reporting similar problems with some other characters.
回答2:
There are issues creating bindings when a property name contains some reserved characters like .
and /
. The property path parser just doesn't handle them. You have a couple options:
Instead of having the grid automatically generate the columns, define them yourself, and for columns with reserved characters, bind to the data row's indexer instead of the column name:
<DataGridTextColumn Binding="{Binding Path=[request/sec]}" Header="request/sec" />
If you only care about the column name because you want to control the column header text, just declare the columns manually, give the column a "legal" name, and set the
Header
just like above.
来源:https://stackoverflow.com/questions/26537983/wpf-datatable-column-name-can-not-have-slash