0

我正在尝试从 XAML DataGrid 更新 RowEditEnding 事件数据库名称上的数据库记录:ManageOrders 表:客户:ID (Guid)、名称 (varchar)、地址 (varchar) 数据库设置为:如果较新数据集设置为则复制:不要复制

如果我使用 UPDATE 和 ExecuteNonQuery 对查询进行硬编码,我会得到结果,但是当我尝试使用 datasets / dataTableAdapter.Update 时它不会更新。请帮忙。请注意,我对 WPF 和 C# 编程非常陌生。我花了一周的时间研究这个,找不到答案或自己解决。

这是我的代码

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


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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        manageOrdersDataSet = ((SimpleData.ManageOrdersDataSet)(this.FindResource("manageOrdersDataSet")));
        // Load data into the table Customer. You can modify this code as needed.
        manageOrdersDataSetCustomerTableAdapter = new SimpleData.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)        
{
   this.manageOrdersDataSetCustomerTableAdapter.Update(this.manageOrdersDataSet.Customer);
        manageOrdersDataSet.AcceptChanges();   

 }
}

这是我的 XAML

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SimpleData" x:Class="SimpleData.MainWindow"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

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

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="533*"/>
        <ColumnDefinition Width="186*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="43*"/>
        <RowDefinition Height="377*"/>
    </Grid.RowDefinitions>
    <TabControl  Grid.Column="0" HorizontalAlignment="Left" Height="357" Margin="10,9.8,0,0" Grid.Row="1" VerticalAlignment="Top" Width="513">
        <TabItem x:Name="tabView" Header="Customer">
            <Grid DataContext="{StaticResource customerViewSource}">

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

            </Grid>
        </TabItem>

        <TabItem Header="Order">
            <Grid Background="#FFE5E5E5" Width="auto"/>
        </TabItem>
    </TabControl>


</Grid>

4

1 回答 1

0

我找到了我的问题的答案。当我创建我的数据库时,我忘记设置 PRIMARY KEY。我的意思是我没有去属性和选定的字段 - 主键(在 MSSQL 管理器中,表的设计)没有设置 Visual Studio 无法正确构建 UPDATE 命令

验证我是否转到(右键单击)表适配器 -> 配置 -> 高级选项并检查刷新数据表是否已打开。

希望有一天这对某人有所帮助。

于 2015-03-03T15:29:11.110 回答