问题
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.
回答1:
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)
回答2:
I'm unable to work out the accepted answer. My situation is identical to the one described here: 2 tables, relationship, 2 datagrids.
I have looked into the CreateChildView method, but I'm missing a key point somewhere. My current code for getting the childview:
public DataView ChildView {
get {
return ParentChildRelation.ParentTable.DefaultView[0].CreateChildView("regels")
}
}
The problem is two-fold, firstly I obviously have the index fixed, meaning I always get the childview of the first row of the parenttable. Secondly, I am failing miserably in fetching a new child view when a new row is selected in the parent view (ChildView only gets called on initialization).
My XAML:
<Grid>
<DataGrid
ItemsSource="{Binding Path=ParentView}"
AutoGenerateColumns="True"
Name="dataGrid1"
/>
<DataGrid
ItemsSource="{Binding Path=ChildView}"
AutoGenerateColumns="True"
Name="dataGrid2"
/>
</Grid>
I'd like to make this a XAML solution and avoid writing eventhandlers updating the childview. I've tried playing around with DataContext but got nowhere, documentation on how to utilze this structure with datasets and relations is really slim (makes me wonder if I'm going down the right path).
来源:https://stackoverflow.com/questions/10126602/how-to-display-related-data-from-dataset-in-wpf-datagrid