c# - Binding DataGridView to database with EF5 classes not sending changes to database -
i using entity framework 5.0 + .net 4.5
i have created database using ef model first approach , want bind datagridview
database using ef classes changes database or in datagridview
synchronized automatically.
this code:
//form level fields private bindinglist<product> _products; private bindingsource _productsource = new bindingsource(); ... in form load event //load data database using ef classes var tmp = _context.basecategoryset.oftype<product>().tolist(); //converting 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, changes not sent database - missing?
additional things did in hope find solution...
1) added save button call updating grid control's data database explicitly, code:
_productsource.endedit(); _context.savechanges();
->this did not result in storing new record database
2) added code add new records bunch of controls individual record properties (textboxes, datepickers)
var x = _context.basecategoryset.create<product>(); //settting x properties values //that set in aforementioned individual controls _context.basecategoryset.add(x); _context.savechanges();
-->when add new records using technique - stored in database, once more strange behavior - new record not automatically loaded in grid control (but should be, since databinding grid corresponding ef dbset...)
and 1 more strangeness - updates have made in datagridview
control records loaded database - updates sent database...
3) switched devexpress xtragrid
stadard datagridview
control, did not help...
i have searched through tons of topics regarding ef databinding no success...
don't know if matters, using inheritance in entity model: product
derives unifofsales
, unitofsales
derives basecategory
class.
one more thing tried
i tried (..).local.tobindinglist suggested ladislav mrnka in post how make 2 way-databinding using ef in winforms?
well did send changes database, changes stored in base class table (basecategory), there table derived class well. code used binding
_context.basecategoryset.oftype<product>.load(); //i tried use derived class oftype<product> ensure compiler //knows instance of derived class (product), //not base class basecategory, //but can not "local" working oftype... productgridview.datasource = _context.basecategoryset.local.tobindinglist();
i did find solution!
i had add dbset products in dbcontext class ef model derived class (product, derived basecategory class)
after use
productgridview.datasource=_context.products.local.tobindinglist();
Comments
Post a Comment