GridView - 在新行提交上创建多个表

时间:2013-12-17 23:08:13

标签: wpf entity-framework

我有一个绑定到EF表格的网格。我最近更改了设计,但我得到了以下异常。

Entities in 'CFETSDBContainer.FREQUENCIES' participate in the 'FREQUENCYFreqPool' relationship. 0 related 'FreqPool' were found. 1 'FreqPool' is expected.

为什么这样做很明显,但我不知道如何修复它。当用户在数据网格中输入新行并提交它时,它还需要创建新的FREQ_POOL和FREQ_TYPE条目。

FREQ_POOL和FREQ_TYPE是基类,所以我需要手动设置两者的类型,并在我认为的代码隐藏中设置它们的属性。

我该怎么做?我只做了简单的数据绑定。这是我的代码。

XAML:

<DataGrid  AutoGenerateColumns="False" Height="278" HorizontalAlignment="Left" Margin="24,31,0,0" Name="dgIA" VerticalAlignment="Top" Width="520" Loaded="dataGrid1_Loaded" RowEditEnding="dgIA_RowEditEnding" DataContext="{Binding}">
        <DataGrid.Columns>

            <DataGridComboBoxColumn DisplayMemberPath="Name" Header="GACC" SelectedValuePath="{Binding Name}" x:Name="cbGACCS" SelectedValueBinding="{Binding ZONE.GACC,Mode=TwoWay}" />            
            <DataGridComboBoxColumn DisplayMemberPath="Name" Header="Zones" SelectedValuePath="{Binding Name}" x:Name="cbZONES" SelectedValueBinding="{Binding ZONE,Mode=TwoWay}" />                
            <DataGridTextColumn Header="Frequency" Binding="{Binding Path=Frequency,Mode=TwoWay}"/>

            <DataGridComboBoxColumn Header="Frequency Type" x:Name="cbFT"  SelectedValueBinding="{Binding Path=FrequencyType,Mode=TwoWay}"/>
            <DataGridComboBoxColumn  Header="Owner" x:Name="cbOwner" SelectedValueBinding="{Binding Path=Owner,Mode=TwoWay}" />  

            <DataGridTextColumn Header="Usage Type" Binding="{Binding Path=UsageType,Mode=TwoWay}"/>
            <DataGridTextColumn Header="Comments" Binding="{Binding Path=Comments,Mode=TwoWay}"/>
            <DataGridTextColumn Header="Serial #" Binding="{Binding Path=Serial,Mode=TwoWay}"/>

        </DataGrid.Columns>
    </DataGrid>

代码隐藏:

    private void dgIA_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        if (!isManualEditCommit)
        {
            isManualEditCommit = true; 

                DataGrid grid = (DataGrid)sender;
                grid.CommitEdit(DataGridEditingUnit.Row, true);
                ctx.SaveChanges();

            isManualEditCommit = false;
        }      

    }
}

enter image description here

0 个答案:

没有答案