我正在尝试在RowEditEnding事件上更新XAML DataGrid中的数据库记录 数据库名称: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>
答案 0 :(得分:0)
我找到了问题的答案。 当我创建我的数据库时,我忘记设置PRIMARY KEY。 我的意思是我没有去属性和选定的字段 - 主键(在MSSQL管理器,表的设计) 没有设置Visual Studio无法正确构建UPDATE命令
验证我去了(右键单击)表适配器 - &gt;配置 - &gt;提前选项并检查REFRESH THE DATA TABLE是否已打开。
希望有一天这会对某人有所帮助。