Unable to display data in a WPF datagrid that has

2019-02-27 05:44发布

问题:

I am using a DataGrid object from the WPF toolkit. I am binding the DataGrid object to the default view of a DataTable instance as declared in the following code:

WeatherGrid.ItemsSource = weatherDataTable.DefaultView;

weatherDataTable has three columns. The first column is defined to contain a string data type. The other two columns are defined to contain double data types. When the application exits the function that calls the binding as expressed in the declaration, The DataGrid object displays data for the first column, but not the other columns. When I type the following in the immediate window in the debugger:

((DataRowView)WeatherGrid.Items[0]).Row[1]

I get a number, but this doesn't correspond with what is being displayed. Why is only the first column visible, and how can I get all of the data to be visible? I'll leave my XAML definition for the DataGrid object below:

<toolkit:DataGrid Margin="12.726,77.71,12,0" Name="WeatherGrid" Height="500" Grid.Row="1" VerticalAlignment="Top" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" />

回答1:

It turns out that for my second and third columns, I have been using perverse column names. I ultimately wanted my DataTable to be dynamic in that it could have a variable number of columns, and I wanted to uniquely identify each column by date. So, I appended two strings, one with a name and one with numbers delimited by "/". It seems that this caused some binding problems with a DataGrid object.

In order to solve this problem, I changed the string uniquely identifying the date with a number for the day in the year and the year. As a result, I was able to display the data.

It is interesting to note that there are no such problems when using slashes in DataTable column names when binding to GridViews in ASP.NET.



回答2:

In fact the column Binding name is case sensitive and must match the underlying dataview column names exactly.