I am trying to do this for my project Entity Framework Databinding with WinForms but instead of using single form, I'm using 2 forms.
CategoryDataGridView
is displayed on the Form1
, then there is an edit button on Form1
, which will load the Form2 which has the ProductsDataGridView
(product list of selected category) and a save button to save the changes
I have the following code on my Form2
ProductContext _context = new ProductContext();
public int SeletedCategID { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
_context.Products.Where(c => c.CategoryId == SeletedCategID).ToList();
productsBindingSource.DataSource = (_context.Products.Local).ToList();
}
private void SaveBtn_Click(object sender, EventArgs e)
{
this.Validate();
foreach (var product in _context.Products.Local.ToList())
{
if (product.Category == null)
{
_context.Products.Remove(product);
}
}
this._context.SaveChanges();
this.productsDataGridView.Refresh();
Form1 frm1 = (Form1)Application.OpenForms["Form1"];
frm1.Activate();
frm1.Refresh();
this.Dispose();
}
my problem now is only edits on the products are saved to DB. Add & Delete are not saved to database.