how to update database through tableadpater in WPF?
in deigner i set tableadapter update command as follows, after using tableadpater update in button event, it do not update, how to update in WPF?
I have added a loop to update dataset manually and ensure dataset is updated with messagebox.show however after using dataadapter.update the assign to datacontext again, the result do not change, means database not update
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();
}
It's not surprise. In the beginning you set: FoodDataSet.AcceptChanges(); "When you call AcceptChanges on the DataSet, any DataRow objects still in edit-mode end their edits successfully. The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed."
And then you try: foodTableAdapter.Update(FoodDataSet.Food); After AcceptChanges() there are nothing to save to db.
精彩评论