2

我有一个包含两个表的数据集,一个简单的一对多父子关系。例如;

Parent Table
ParentId (int)
Name     (string)

Child Table
ChildId (int)
ParentId (int)
Name     (string)

在这个数据集中,我在父 id 字段上建立了父子关系(ParentChildRelation)。如果我以编程方式进行 Parent.getChildRows() 调用,我会看到正确的相关子记录,因此我知道关系很好。

我需要显示一个带有父行列表的数据网格,并让另一个单独的数据网格显示所选父行的子记录列表。

到目前为止,在我后面的代码窗口中,我有

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"] ;
        }
    }

我还在窗口构造函数中将窗口的数据上下文设置为自身,例如;

this.DataContext = this;

在我的 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>

但是我无法让任何子记录显示在 dataGrid2 中。

我将此网格绑定到数据关系名称是正确的吗?

如果我绑定到我的窗口上的另一个属性;

    public DataView ChildrenView
    {
        get
        {
            return this.ParentChildDataSet.Tables["Child"].DefaultView;
        }
    }

然后我会看到所有记录,无论父母如何,正如您所期望的那样。

感谢任何人都可以给我的任何指示。

4

1 回答 1

0

当您返回 DataView 时,它正在工作但显示全部。

在这里你返回一个 DataRelation

ItemsSource="{Binding Path=ParentChildRelation}" 

您需要返回一个 DataView

使用关系生成 DataView

DataRowView.CreateChildView 方法(DataRelation)

于 2012-04-12T16:08:19.343 回答