RowEditEnding和DataGrid更新

时间:2015-03-04 01:44:24

标签: c# wpf datagrid

我有WPF dataGrid;当我使用RowEditEnding事件更新我的数据库时,它无法正常运行。 1.我输入新值,在我分配ID的代码中,当我回到网格时,我没有看到我的ID,只有我输入的值。 (如果我关闭申请,数据会丢失) 2.当我再次双击datagrid并保留行时,一切都按照应有的方式运行,数据会更新并保存在DB中。

我正在使用正确的方法吗?

这是代码:

    <Window.Resources>
        <local:ManageOrdersDataSet x:Key="manageOrdersDataSet"/>
        <CollectionViewSource x:Key="customerViewSource" Source="{Binding Customer, Source={StaticResource manageOrdersDataSet}}"/>
    </Window.Resources>

<Grid DataContext="{StaticResource customerViewSource}">

                    <DataGrid x:Name="customerDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="0,0,-0.2,0.4" ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False" RowEditEnding="customerDataGrid_RowEditEnding">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="iDColumn" Width="10" Header="ID" Binding="{Binding ID,  Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn x:Name="nameColumn" Width="*" Header="Name" Binding="{Binding Name, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn x:Name="addressColumn" Width="*" Header="Address" Binding="{Binding Address,  Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>

和c#部分

OrderManager.ManageOrdersDataSet manageOrdersDataSet;
        OrderManager.ManageOrdersDataSetTableAdapters.CustomerTableAdapter manageOrdersDataSetCustomerTableAdapter;
        System.Windows.Data.CollectionViewSource customerViewSource;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            manageOrdersDataSet = ((OrderManager.ManageOrdersDataSet)(this.FindResource("manageOrdersDataSet")));
            // Load data into the table Customer. You can modify this code as needed.
             manageOrdersDataSetCustomerTableAdapter = new OrderManager.ManageOrdersDataSetTableAdapters.CustomerTableAdapter();
            manageOrdersDataSetCustomerTableAdapter.Fill(manageOrdersDataSet.Customer);
            customerViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("customerViewSource")));
            customerViewSource.View.MoveCurrentToFirst();

        }
 private void customerDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
 if (rowView != null)
            {

                ManageOrdersDataSet.CustomerRow row = rowView.Row as ManageOrdersDataSet.CustomerRow;

                if (row.IsNull("Name"))
                {
                    row["Name"] = "Defailt";
                }
                if (row.IsNull("Address"))
                {
                    row["Address"] = "Address Def";
                }
                if (row.IsNull("ID"))
                {
                    row["ID"] = Guid.NewGuid();
                }
   this.manageOrdersDataSetCustomerTableAdapter.Update(row);
            }
           }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 我无法解释它,但显然在事件 RowEditEnding 时,新数据行不被视为具有值或已更改。 所以我决定调用事件处理程序。

这是步骤

  1. 将事件处理程序添加到Windows_Loaded

    manageOrdersDataSet.Customer.RowChanged + = new DataRowChangeEventHandler(Row_Changed);             manageOrdersDataSet.Customer.RowDeleted + = new DataRowChangeEventHandler(Row_Changed);

  2. 实现事件处理程序

    public void Row_Changed(object sender,DataRowChangeEventArgs e)     {

        try
        {
            this.manageOrdersDataSetCustomerTableAdapter.Update(manageOrdersDataSet.Customer);
        }
        catch
        {
            // do something
        } 
    

    }