Reputation: 4907
I've got a DataGrid, as defined below:
<DataGrid Name="dgResults"
IsReadOnly="True"
AutoGenerateColumns="True"
AllowDrop="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserResizeRows="False"
CanUserSortColumns="False"
ColumnWidth="120"
Margin="15,10,10,10"
Visibility="Collapsed"
ItemsSource="{Binding}"/>
For some reason, no data is showing when binding to it. The correct amount of rows are displaying, however they are all empty. This is my code:
dgResults.DataContext = dtTopTwoHundredResults.AsDataView();
dgResults.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dataGrid_AutoGeneratingColumn);
dgResults.Visibility = Visibility.Visible;
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
//Sets the DataGrid's headers to be TextBlocks, which solves a problem whereby underscore characters in the header are ignored.
TextBlock block = new TextBlock();
block.Text = e.Column.Header.ToString();
e.Column.Header = block;
}
It's definitely not a problem with the data source, as the data is contained inside it as it should be. It's just the DataGrid. This is how it's displaying, i.e. the correct amount of rows but no data:
I used Snoop to find out if the text is actually contained in the DataGrid, and I'm getting this:
System.Windows.Data Error: 40 : BindingExpression path error: 'Employee identification number' property not found on 'object' ''DataRowView' (HashCode=51298929)'. BindingExpression:Path=Employee identification number. Foreign key to Employee.BusinessEntityID.; DataItem='DataRowView' (HashCode=51298929); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Employee identification number' property not found on 'object' ''DataRowView' (HashCode=51298929)'. BindingExpression:Path=Employee identification number. Foreign key to Employee.BusinessEntityID.; DataItem='DataRowView' (HashCode=51298929); target element is 'PropertyInformation' (HashCode=11239682); target property is 'Value' (type 'Object')
Upvotes: 1
Views: 217
Reputation: 4907
The problem over here is the fact that punctuation is involved in the column headers. Removing the punctuation solved the issue. Hopefully someone with the same problem will read this, as it took me many hours to realise.
Upvotes: 4
Reputation: 1608
You could explicitly set your Data Grid Columns as so, problem at that point your data grid is limited in what it can display cause headers are explicitly set. The problem is when you are setting the headers in your autogeneratedColumn event handler the text block is ignoring the underscore like you said but that point wpf doesnt know how to bind column data to that header. That's why you are getting error with the binding path. The "Employee identification number" is what its trying to bind back into your dataview, but in your dataview its "Employee_identification_number". The solution below solves your problem but limits you to only displaying that particular dataview. If thats not an issue then this is your solution.
<DataGrid.Columns>
<DataGridTextColumn Header="Employee identification number"
Binding="{Binding Path=EmployeeID}"
</DataGridTextColumn>
<Insert other column definitions here/>
</DataGrid.Columns>
Upvotes: 0
Reputation: 316
From your code, it looks like you just want custom column headers. If so, you'll probably want to define your columns explicitly in the XAML:
<DataGrid.Columns>
<DataGridTextColumn Header="Employee identification number"
Binding="{Binding Path=EmployeeID}"
</DataGridTextColumn>
<Insert other column definitions here/>
</DataGrid.Columns>
The way you have it now, it seems that the XAML parser (or whatever) is looking for property names that match what's in the column header (or something like that), and it isn't finding properties on your ItemsSource objects that match the column header.
Upvotes: 0