我正在尝试从 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>