How can I use data binding on a details form to add a new entity to my data source? I have created an Object data source, and dragged it onto my form as ‘details’, which automatically adds a bindingSource and bindingNavigator control to my form, in addition to data bound detail fields. I have the following code which all executes with no exceptions, but no new Branch record gets added when I click ‘Add New’ and then ‘Save’.
private void BranchEditForm_Load(object sender, EventArgs e)
{
branchBindingSource.DataSource = _loansEntities.Branches;
}
private void branchBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
_loansEntities.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
I can’t seem to find any Entity Framework data binding examples that don’t just default to grids.
Adding a new record generated through a details form can be done by calling the the relevant add method in the model before calling the
SaveChangesmethod to commit the record to data source.For the above, this code might solve the problem, presuming there is a method called
AddToBrancheswhich takes aBranchobject as an argument.