2

I have simple C# WPF application which gets the data from the database into WPF datagrid via an Entity Framework model. In direction from database to WPF DataGrid everything works fine.

My problem is the opposite direction from datagrid to the database. I'd like to update the database after every cell update, so I work in CellEditEnding event.

The code below doesn't throw any exception but no WPF datagrid cell changes are saved into the database (DGISPRSDataView is my WPF datagrid)

Please can you tell me, where is the problem? What am I missing? How can I connect datagrid changes into data model and to database? Or is there any callback from server - as is in Silverlight case - (Entity model + DomainContext class)

Thanks a lot!

private void DGISPRSDataView_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
   try
   {
      ReviewsEntities dataEntity = new RreviewsEntities(); //DataContext
      this.DGISPRSDataView.DataContext = dataEntity.Form_output_test;
      dataEntity.SaveChanges();

      MessageBox.Show("Update succesfully end");
   }
   catch (Exception err)
   {
      MessageBox.Show(err.ToString());
   }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
VilemRousi
  • 2,082
  • 4
  • 24
  • 34

1 Answers1

1

Try loading your data to one of the DBSets inside the context. A context should have a DBSet for each class you want to maintain, and the grid should have one of them as it's datasource

For example when loading

        var dset = Db.Tasks;
        DbSet<Task> qry = dset;
        qry.Load();
        bindingSource1.DataSource  =dset.Local.ToBindingList();

When saving

        bindingSource1.EndEdit();
        Db.SaveChanges();
Kirsten
  • 15,730
  • 41
  • 179
  • 318