Prokurors
Prokurors

Reputation: 2548

Binding DataGridView to database with EF5 classes not sending changes to database

I am using Entity Framework 5.0 + .NET 4.5

I have created database using EF Model first approach and I want to bind DataGridView to database using EF classes so that any changes to database or in the DataGridView are synchronized automatically.

This is my code:

//form level fields
private BindingList<Product> _products;
private BindingSource _productSource = new BindingSource(); 

... in the form load event
//load the data from database using EF classes
var tmp = _context.BaseCategorySet.OfType<Product>().ToList();

//converting to IBindingList
_products = new BindingList<Product>(tmp);
_products.AllowEdit = true;
_products.AllowNew = true;

_productSource.DataSource = _products; 

//setting GridControl's data source
ProductGrid.DataSource = _productSource;

I can add new rows or change data, but those changes are not sent to the database - what am I missing?

Additional things I did in hope to find a solution...

1) I added a Save button to call updating the grid control's data to database explicitly, with code:

_productSource.EndEdit();
_context.SaveChanges();

->this did not result in storing the new record into the database

2) I added a code to add new records with a bunch of controls for individual record properties (Textboxes, DatePickers)

var x = _context.BaseCategorySet.Create<Product>();
//settting all the x properties with values
//that are set in aforementioned individual controls

_context.BaseCategorySet.Add(x);
_context.SaveChanges();

-->when I add new records using this technique - it IS stored in the database, but once more a strange behavior - this new record is not automatically loaded in the grid control (but should be, since I am databinding grid to the corresponding EF DbSet...)

And one more strangeness - updates that I have made in the DataGridView control to the records that were loaded to database - those updates ARE sent to database...

3) I switched from DevExpress XtraGrid to stadard DataGridView control, but that did not help...

I have searched through tons of topics regarding EF databinding with no success...

Don't know if this matters, but I am using inheritance in my Entity model: Product derives from UnifOfSales and UnitOfSales derives from BaseCategory class.

One more thing i tried

I tried the (..).Local.ToBindingList that was suggested by Ladislav Mrnka in this post How to make two way-databinding using EF in winforms?

well it did send changes back to the database, but the changes were stored only in the base class table (BaseCategory), but there is a table for derived class as well. This is the code i used for binding

_context.BaseCategorySet.OfType<Product>.Load(); 
//i tried to use derived class with OfType<Product> to ensure that compiler
//knows that this is instance of derived class (Product), 
//not the base class BaseCategory, 
//but I can not get "Local" working with OfType...
ProductGridView.DataSource = _context.BaseCategorySet.Local.ToBindingList(); 

Upvotes: 1

Views: 707

Answers (1)

Prokurors
Prokurors

Reputation: 2548

I did find the solution!

I had to add DbSet Products in DbContext class for EF model derived class (Product, derived from BaseCategory class)

After this I could use

ProductGridView.DataSource=_context.Products.Local.ToBindingList();

Upvotes: 1

Related Questions