Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding DataGridView to database with EF5 classes not sending changes to database
    text
    copied!<p>I am using Entity Framework 5.0 + .NET 4.5</p> <p>I have created database using EF Model first approach and I want to bind <code>DataGridView</code> to database using EF classes so that any changes to database or in the <code>DataGridView</code> are synchronized automatically.</p> <p><strong>This is my code:</strong></p> <pre><code>//form level fields private BindingList&lt;Product&gt; _products; private BindingSource _productSource = new BindingSource(); ... in the form load event //load the data from database using EF classes var tmp = _context.BaseCategorySet.OfType&lt;Product&gt;().ToList(); //converting to IBindingList _products = new BindingList&lt;Product&gt;(tmp); _products.AllowEdit = true; _products.AllowNew = true; _productSource.DataSource = _products; //setting GridControl's data source ProductGrid.DataSource = _productSource; </code></pre> <p>I can add new rows or change data, but those changes are not sent to the database - what am I missing?</p> <p><strong>Additional things I did in hope to find a solution...</strong></p> <p>1) I added a Save button to call updating the grid control's data to database explicitly, with code:</p> <pre><code>_productSource.EndEdit(); _context.SaveChanges(); </code></pre> <p>->this did not result in storing the new record into the database</p> <p>2) I added a code to add new records with a bunch of controls for individual record properties (Textboxes, DatePickers)</p> <pre><code>var x = _context.BaseCategorySet.Create&lt;Product&gt;(); //settting all the x properties with values //that are set in aforementioned individual controls _context.BaseCategorySet.Add(x); _context.SaveChanges(); </code></pre> <p>-->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...)</p> <p>And one more strangeness - updates that I have made in the <code>DataGridView</code> control to the records that were loaded to database - those updates ARE sent to database...</p> <p>3) I switched from DevExpress <code>XtraGrid</code> to stadard <code>DataGridView</code> control, but that did not help...</p> <p>I have searched through tons of topics regarding EF databinding with no success...</p> <p>Don't know if this matters, but I am using inheritance in my Entity model: <code>Product</code> derives from <code>UnifOfSales</code> and <code>UnitOfSales</code> derives from <code>BaseCategory</code> class.</p> <p><strong>One more thing i tried</strong></p> <p>I tried the (..).Local.ToBindingList that was suggested by Ladislav Mrnka in this post <a href="https://stackoverflow.com/questions/12736958/how-to-make-two-way-databinding-using-ef-in-winforms">How to make two way-databinding using EF in winforms?</a></p> <p>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</p> <pre><code>_context.BaseCategorySet.OfType&lt;Product&gt;.Load(); //i tried to use derived class with OfType&lt;Product&gt; 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(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload