I've got a dataset with two tables, a simple one to many parent child relationship going on. eg;
Parent Table
ParentId (int)
Name (string)
Child Table
ChildId (int)
ParentId (int)
Name (string)
In this dataset I've got a relationship (ParentChildRelation) set up between the parent and child on the parent id field. And if I programtically do a Parent.getChildRows() call I see the correct related child records so I know the relationship is good.
I need to display a datagrid with a list of the parent rows and have another separate datagrid displaying the list of child records for the selected parent.
So far on my window in code behind I have
private DataSet _parentChildDataSet;
public DataSet ParentChildDataSet
{
get
{
return _parentChildDataSet;
}
set
{
_parentChildDataSet = value;
}
public DataView ParentView
{
get
{
return this.ParentChildDataSet.Tables["Parent"].DefaultView;
}
}
public DataRelation ParentChildRelation
{
get
{
return this.ParentChildDataSet.Relations["Parent_Child"] ;
}
}
I also set the datacontext for the window to itself in the windows constructor eg ;
this.DataContext = this;
and in my xaml
<Grid>
<DataGrid ItemsSource="{Binding Path=ParentView}" AutoGenerateColumns="True" Name="dataGrid1">
</DataGrid>
<DataGrid ItemsSource="{Binding Path=ParentChildRelation}" Name="dataGrid2" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" />
</DataGrid.Columns>
</DataGrid>
</Grid>
However I cannot get any child records to show up in dataGrid2.
I'm binding this grid to the datarelation name is this correct?
if I bind to another property on my window ;
public DataView ChildrenView
{
get
{
return this.ParentChildDataSet.Tables["Child"].DefaultView;
}
}
then I see all the records regardless of the parent as you would expect.
Thanks for any pointers anyone can give me.
When you return a DataView it is working but displays all.
Here you are returning a DataRelation
ItemsSource="{Binding Path=ParentChildRelation}"
You need to return a DataView
Use the relation to produce the DataView
DataRowView.CreateChildView Method (DataRelation)