WPF主 - 详细信息新数据从数据网格中消失

时间:2014-06-10 18:02:43

标签: wpf data-binding

我正在使用一个WPF项目,该项目在一个窗口中包含一对绑定到ADO数据集的数据网格,该数据集包含一个主 - 详细信息关系中的两个表。数据集配置为将数据关系设置为“级联”。显示数据工作正常;在主网格中选择一行将在详细信息网格中显示相应的行。

但是,在主网格中添加一个新行,然后在详细信息网格中添加一行或多行然后保存,会导致新的详细信息行从网格中消失。

断开并检查可视化器中的数据集表明数据是正确的; PK和FK已从数据库正确更新。重新运行程序会正确显示数据,并且检查数据集似乎显示与未显示数据时相同的值。 使用TableAdapterManager保存或在tableadapters上使用单独的Update调用没有任何区别。事实上,在网上搜索这个问题已经让我得到了一些未解决的问题,这些问题表明EF也会发生同样的事情,而在Winforms中做同样的事情也能正常工作 - 所以它似乎是一个WPF问题。任何想法的家伙?

使用Northwnd证明这一点的代码:

  public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        MasterDetailTest.dsOrders dsOrders = ((MasterDetailTest.dsOrders)(this.FindResource("dsOrders")));
        // Load data into the table Orders. You can modify this code as needed.
        MasterDetailTest.dsOrdersTableAdapters.OrdersTableAdapter dsOrdersOrdersTableAdapter = new MasterDetailTest.dsOrdersTableAdapters.OrdersTableAdapter();
        dsOrdersOrdersTableAdapter.Fill(dsOrders.Orders);
        System.Windows.Data.CollectionViewSource ordersViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ordersViewSource")));
        ordersViewSource.View.MoveCurrentToFirst();
        // Load data into the table Order_Details. You can modify this code as needed.
        MasterDetailTest.dsOrdersTableAdapters.Order_DetailsTableAdapter dsOrdersOrder_DetailsTableAdapter = new MasterDetailTest.dsOrdersTableAdapters.Order_DetailsTableAdapter();
        dsOrdersOrder_DetailsTableAdapter.Fill(dsOrders.Order_Details);
        System.Windows.Data.CollectionViewSource ordersOrder_DetailsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ordersOrder_DetailsViewSource")));
        ordersOrder_DetailsViewSource.View.MoveCurrentToFirst();
    }

    private void cmdSave_Click(object sender, RoutedEventArgs e)
    {
        MasterDetailTest.dsOrders dsOrders = ((MasterDetailTest.dsOrders)(this.FindResource("dsOrders")));
        dsOrdersTableAdapters.TableAdapterManager tam = new dsOrdersTableAdapters.TableAdapterManager();
        MasterDetailTest.dsOrdersTableAdapters.OrdersTableAdapter taOrders = new dsOrdersTableAdapters.OrdersTableAdapter();
        MasterDetailTest.dsOrdersTableAdapters.Order_DetailsTableAdapter taDetail = new dsOrdersTableAdapters.Order_DetailsTableAdapter();
        tam.OrdersTableAdapter = taOrders;
        tam.Order_DetailsTableAdapter = taDetail;
        tam.UpdateAll(dsOrders);

        //taOrders.Update(dsOrders.Orders);
        //taDetail.Update(dsOrders.Order_Details);

    }
}

XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MasterDetailTest" x:Class="MasterDetailTest.MainWindow"
    Title="MainWindow" Height="425" Width="912" Loaded="Window_Loaded">
<Window.Resources>
    <local:dsOrders x:Key="dsOrders"/>
    <CollectionViewSource x:Key="ordersViewSource" Source="{Binding Orders, Source={StaticResource dsOrders}}"/>
    <CollectionViewSource x:Key="ordersOrder_DetailsViewSource" Source="{Binding FK_Order_Details_Orders, Source={StaticResource ordersViewSource}}"/>
</Window.Resources>
<Grid DataContext="{StaticResource ordersViewSource}">
    <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="309" Width="440" ItemsSource="{Binding}"/>
    <DataGrid x:Name="dgDetail" HorizontalAlignment="Left" Margin="455,10,0,0" VerticalAlignment="Top" Height="309" Width="439" ItemsSource="{Binding Source={StaticResource ordersOrder_DetailsViewSource}}"/>
    <Button x:Name="cmdSave" Content="Save" HorizontalAlignment="Left" Margin="819,350,0,0" VerticalAlignment="Top" Width="75" Click="cmdSave_Click"/>

</Grid>

0 个答案:

没有答案