在deigner中我按如下方式设置tableadapter update命令,在按钮事件中使用tableadpater更新后,它不更新,如何在WPF中更新?
我添加了一个循环来手动更新数据集,并确保使用messagebox.show更新数据集 但是在使用dataadapter.update再次分配给datacontext之后,结果不会改变,意味着数据库没有更新
UPDATE Food
SET Home =@Home
WHERE Name=@Name
<local:Food_DataSet x:Key="Food_DataSet" />
</Window.Resources>
<Grid VerticalAlignment="Top" Height="300">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="150"/>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Menu Grid.Row="0" Height="20" VerticalAlignment="Top">
<MenuItem Header="Archivo" Background="Wheat" Name="MenuItem1" >
<MenuItem Name="mi1" Style="{StaticResource Triggers}" Header="_Nuevo" />
<MenuItem Name="mi2" Style="{StaticResource Triggers}" Header="_Abrir" />
<MenuItem Name="mi3" Style="{StaticResource Triggers}" Header="_Salir"/>
</MenuItem>
</Menu>
<ListView Grid.Row="1"
Name="listView"
Margin="5,5,5,5"
ItemsSource="{Binding Mode=OneWay}"
ItemContainerStyle="{DynamicResource myItemContainerStyle}"
ItemsPanel="{DynamicResource ItemsPanelTemplate1}"
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}"
Header="{Binding Path=Columns[Name], Converter={StaticResource columnHeaderConverter}}"/>
<GridViewColumn Header="{Binding Path=Columns[Home], Converter={StaticResource columnHeaderConverter}}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<lib:EditBox
BorderThickness="0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Value="{Binding Home, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
...
private WordProcess.Food_DataSetTableAdapters.FoodTableAdapter foodTableAdapter;
private WordProcess.Food_DataSet FoodDataSet;
public Window1()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
DataContext = (new FoodTableAdapter()).GetData();
FoodDataSet = ((WordProcess.Food_DataSet)(this.FindResource("Food_DataSet")));
foodTableAdapter = new WordProcess.Food_DataSetTableAdapters.FoodTableAdapter();
//foodTableAdapter.Fill(FoodDataSet.Food);
}
private void word_button_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult objResult = MessageBox.Show("Are You sure You want to save", "Save Confirmation", MessageBoxButton.YesNo);
if(objResult == MessageBoxResult.Yes)
{
for (int i = 0; i < FoodDataSet.Food.Count; i++)
{
for(int j=0; j<listView.Items.Count; j++)
{
DataRowView Home = (DataRowView)listView.Items[j];
FoodDataSet.Food[i].Home = Home.Row[1].ToString();
}
}
FoodDataSet.AcceptChanges();
MessageBox.Show(FoodDataSet.Food[0].Home);
try
{
foodTableAdapter.Update(FoodDataSet.Food);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
DataContext = foodTableAdapter.GetData();
}
答案 0 :(得分:0)
这并不奇怪。 在开头你设置:FoodDataSet.AcceptChanges(); “当您在DataSet上调用AcceptChanges时,任何仍处于编辑模式的DataRow对象都会成功结束编辑。每个DataRow的RowState属性也会更改;添加和修改的行将变为Unchanged,并删除已删除的行。”
然后你尝试:foodTableAdapter.Update(FoodDataSet.Food); 在AcceptChanges()之后,没有什么可以保存到db。