Datagrid Tableadapter更新方法失败

时间:2015-12-08 23:24:26

标签: c# wpf datagrid tableadapter

我将一个表放到XAML窗口上,然后修改代码以使用datagrid上的roweditending事件来更新本地sql数据库。它永远不会奏效。我不会添加,更新或删除数据库表中的任何记录。

这是xaml代码 -

<Grid DataContext="{StaticResource attenderTableViewSource}">
        <DataGrid x:Name="attenderTableDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10,10,10,325" RowDetailsVisibilityMode="VisibleWhenSelected" IsSynchronizedWithCurrentItem="True" RowEditEnding="attenderTableDataGrid_RowEditEnding" >
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="first_NameColumn" Binding="{Binding First Name, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="First Name" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="last_NameColumn" Binding="{Binding Last Name, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Last Name" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="address_Line_1Column" Binding="{Binding Address Line 1, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Address Line 1" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="address_Line_2Column" Binding="{Binding Address Line 2, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Address Line 2" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="address_Line_3Column" Binding="{Binding Address Line 3, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Address Line 3" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="cityColumn" Binding="{Binding City, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="City" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="stateColumn" Binding="{Binding State, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="State" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="zipColumn" Binding="{Binding Zip, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Zip" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="telephoneColumn" Binding="{Binding Telephone, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Telephone" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="_Member_Column" Binding="{Binding Member?, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Member?" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="_Child_Column" Binding="{Binding Child?, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=TwoWay}" Header="Child?" Width="SizeToHeader"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>

这是cs代码

namespace AttendanceRecPro
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
            private AttendanceRecPro.AttendanceRecProDataSet attendanceRecProDataSet = new AttendanceRecProDataSet();
        private AttendanceRecPro.AttendanceRecProDataSetTableAdapters.AttenderTableTableAdapter attendanceRecProDataSetAttenderTableTableAdapter = new AttendanceRecPro.AttendanceRecProDataSetTableAdapters.AttenderTableTableAdapter();
        private System.Windows.Data.CollectionViewSource attenderTableViewSource = new CollectionViewSource();

        public MainWindow()
        {
            InitializeComponent();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            AttendanceRecPro.AttendanceRecProDataSet attendanceRecProDataSet = ((AttendanceRecPro.AttendanceRecProDataSet)(this.FindResource("attendanceRecProDataSet")));
            // Load data into the table AttenderTable. You can modify this code as needed.
            attendanceRecProDataSetAttenderTableTableAdapter.Fill(attendanceRecProDataSet.AttenderTable);
            System.Windows.Data.CollectionViewSource attenderTableViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("attenderTableViewSource")));
            attenderTableViewSource.View.MoveCurrentToFirst();

        }

        private void attenderTableDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {

            if ( e.EditAction == DataGridEditAction.Commit )
            {

                try
                {
                    int rc = 0;

                    rc = attendanceRecProDataSetAttenderTableTableAdapter.Update(attendanceRecProDataSet.AttenderTable);

                    attendanceRecProDataSet.AcceptChanges();

                    MessageBox.Show("Return Code: " + rc);

                }

                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());

                }

                MessageBox.Show("trying to update...");

            }

        }
    }
}

返回码为0,因此不会更新任何行。它永远不会在运行之间保存数据。我还检查了数据集属性是“不复制”并将.mdf文件的版本复制到bin / debug目录中。我花了差不多一个星期来看看这个,看不到我错过了什么。但是,没有更改,添加行或修改的行将更新数据库。网格没有给我一个错误,catch / try块也没有。

此外,数据库中“AttenderTable”上的主键“AttenderID”设置为标识种子1递增1并设置为主键。它应该每次都自动增加,我没有项目,因为它不应该被用户触摸。我不相信这是一个问题,因为数据库应该更新它。

请帮助我应该使用tableadapter更新方法的事件...其他人使用RowEditEnding事件并说它们使它工作但我无法弄清楚我的生活如何。

1 个答案:

答案 0 :(得分:0)

答案是将.update方法移动到datagrid的SelectionChanged事件。见上面的评论。