WinForms - Databinding to 2 tables on 1 form and saving to the database
I have a form that contains information about a client eg/Title, Name a开发者_开发技巧nd their Address eg/Town, Country.
I have a class for each table that has a Save method to update the database.
I am thinking of having 2 bindingsources one for client and one for address. The controls will bind to the relevant bindingsource. When the user clicks the save button it will call EndEdit on each bindingsource and then for the CurrentItem call the Save method.
Does that sound a good approach?
You need to ensure the relation between the classes/tables.
I would make sure you have the appropriate FK constraint defined in your db and then generate Linq2Sql or ADO.Net entity model and bind to that.
After you generate your model, Data>Add new datasource>Object and select the customer object from your entity model.
Make sure you use the dropdown on the datasources tool window to choose 'details' for your customer and the nested address.
drag the customer from the datasources toolwindow to the form.
then drag the address property of the customer from the tool window to the form.
you now have 2 binding sources, one for customer and one for address. the address binding source will use the customer binding source as it's datasource.
you are now covered. edit your form and save.
rolling your own databinding with POCO dtos on a winforms app is just more work than you want to do, especially when the facilities are already provided for you.
精彩评论