Visual Basic: Make a Master/Detail Formset in Visual Studio

Platform:Visual Basic
Task:Make a Master/Detail Formset in Visual Studio
Discussion:
You may need to make a master/detail formset where the parent records are shown in the master form and the related child records are shown in the detail form. You need to have the forms in synchrony. The trick is to refer to the master form's data binding source for the child form instead of creating a new binding source. All tools connected to the same bindingsource will synchronize. This example assumes you are creating the formset using the data connections wizard and dragging the datatable's columns onto the forms as textboxes or gridviews, etc. To have a child form's datagridview synchronize with the master form you need to set the dgv's datasource property to the PARENT form's binding source
Example:
'this is the child form's Load event.  frmMaster is the parent form showing the parent record
Private Sub frmDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'bind a child form's datagridview to the parent form's bindingsource
    Me.DataGridView1.DataSource = frmMaster.TblChildrenBindingSource

    'bind a child form's textbox to the parent form
    Me.TextBox1.DataBindings.Add("Text", frmMain.TblParentsBindingSource, "Name") ' "Name" is the database column's name.  "Text" is the textbox's property to which to bind the data)
    TextBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged 'this seems to be necessary, not sure why or what it does
End Sub
Back to Index