Master/Sub Forms

sfx

Well-known member
Joined
Jan 31, 2006
Messages
46
Programming Experience
Beginner
Hello All,

Does anyone know how to replicate the look and functionality of an MS Access Master/Sub Form? By this I mean not to simply invoke two DataGridViews, but to have the master section displayed with textboxes and comboxes etc while the Sub-form is displayed as a DataGridView... Any ideas?

Cheers

sfx
 
It's not possible with the standard .NET controls. The DataGridView cannot display master/detail relationships in a single grid at all. The DataGrid can but it cannot display data from both tables at the same time. You'd have to use a third-party grid control to get that functionality.
 
When I read this post I interpreted it differently than John, I say:
Yes, you can certainly do that. Create a master detail form with the 2 datagrids on it. Delete the master datagrid and replace it with desired textboxes and comboboxes bound to the fields of the master record. Master navigation will not be that easy but the foward and back controls will still work.
I pictured a split form, textboxes on the top and a datagrid on the bottom. I use one just like it for an address book. Company name and address info in textboxes (master table), and Contact name and phone number in a grid (detail table).

Is that what you want.
 
I think you may be right David. I read the bit about Access abd not using two grids and I interpreted that as meaning that the desire was to have a single grid show the master and detail records like Access does. Now I read it again a little more carefuly I think I was quite wrong. In actual fact, if you want the master records displayed in individual controls and the details records in a grid it is really very simple in 2005. You simply add two BindingSources to your form, one for the master data and one for the detail. You then populate two DataTables in a DataSet and create a DataRelation between them. You then bind the master table to the first BindingSource and the relationship to the second. Then you bind the first BindingSource to the individual controls and the second BindingSource to the grid. Now your master records will be displayed in the individual controls and the detail records for the selected master will be displayed in the grid. You can also add a BindingNavigator linked to the master data to make navigation a snap. This may sound complex but it's really very simple, especially if you're using a typed DataSet. Most of it can be done in the designer.
 
Hi David,

Yes, this is exactly what I wanted! Thanks for getting back to me, I appreciate it.

Actually, after sifting through the help files I came across just such an example. I was also able to implement this example in the way that I wanted. However, I am unsure as to how I might update the underlying database when modifications take place within the DataGridView (details section). Can I still use a CommandBuilder when the DataGridView is bound to a BindingSource? More importantly, is it difficult to take control of the update process in code so that changes in the DataGridView are reflected back to the database?

Cheers,

sfx
 
Hi jmcilhinney,

Thanks for your feedback. I did not think to implement a binding navigator with a master section. Thanks for the tip!

Cheers,

sfx
 
Binding data to controls and updating a database are unrelated operations. They are very often used in tandem but there is no explicit relationship between the two. You use a DataAdapter or TableAdapter to save changes made to data in a DataTable back to its original data source. Whether that table is bound to a control or controls and whether those changes were made in those controls is immaterial.
 
Further, the DataTable acts as the common link between the back-end database and the applications UI. The DataAdapters or TableAdapters will retrieve the data from the database and save any changes back to the database, but they don't care what the Datatable does with the data, how it's displayed or how the changes are made. Likewise, the UI will display the data from the DataTable in controls via data-binding, but it doesn't care where that data came from or what might happen to the changes that are made through the UI. The DataTable acts as a go between so that neither side needs to know or care about the other.
 
Hi jmcilhinney,

Thanks again for your input. Do you happen to have any examples where changes in a DataGridView are propagated back to a database by means of a DataAdapter where the Update command is coded manually? This is an area where I cannot seem to find enough information on.

Take care,

sfx
 
Again, if you're concerned about saving data to a database then forget about the grid as they are not related. DataAdapters and DataGridViews never meet and are blissfully unaware of each other's existence. If you keep thinking of them as related then you make you're life more complex. If you can break big problems up into smaller problems and solve each smaller problem in isolation then the big problem will solve itself. Using a DataAdapter to retrieve data from a database into a DataTable and then saving that data to the database using the same DataAdapter is one small problem. Binding a DataTable to a DataGridView is a separate small problem. You should solve each one individually and in isolation. Once you've done that you can simply fit the two pieces together by using a common DataTable and presto, the big problem is solved. It's like you worrying about getting iron ore out of the ground so you can have a car. You just have to worry about getting the car from the dealer. The dealer gets the car from the manufacturer, who gets the parts from their manufacturers and so on. Put all the little pieces together and at the end you're driving something that used to be rocks in the ground.

Do a member search for techgnome and check the ADO.NET tutorials in his signature.
 
Hi jmcilhinney,

That's sound advice. Thanks.

Actually, I had already resolved this issue. Since all of the changes made in a DataGridView bound to a DataSet were automatically changing the DataSet, all I had to do was update the DataAdapter appropriately. I had been thinking that I needed to catalogue the changed values in a DataGridView in order to pass this information back to the DataAdapter. Thankfully, however, it was never going to be that difficult.

Thank you again for all of your assistance.

Cheers,

sfx
 
Last edited:
Back
Top