Display 2 tables in one Datagridview

viper5646

Active member
Joined
Jan 20, 2009
Messages
38
Location
Canada
Programming Experience
Beginner
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellClick

If DataGridView1.DataMember = "DEsign_tbl" Then
DataGridView1.DataMember = "Customer_tbl"
Exit Sub
End If
If DataGridView1.DataMember = "Customer_tbl" Then
DataGridView1.DataMember = "DEsign_tbl"
End If

End Sub

Hi.

I have a Customers_tbl and a DEsign_tbl from ms Access 2007.
When I click on the Customer cell I would like to display the records
from DEsign_tbl that represent that Customer. Instead of the intire DEsign_tbl, like the coed that I displayed shows.

Thank you
John
 
No the DataGridView no longer allows showing child records. Not sure why they took it out but it used to be allowed with VS 2003 and the DataGrid control.
 
Thank you for your reply

Sorry Tom but while I was watting for a reply I found out that it is possible to
do it. By draging the two tables from the Data Sourse to the Datagridview it realy worked . It gived me the result I was looking for.
Since the coed is hiden I don't know which coed to use. Because instead of having both tables in the same form I would like to have them in seperate forms.
 
Heyya Viper, re-reading your post I think we may not be talking about the exact same thing. Your thread title says "Display 2 tables in one Datagridview" but re-reading your initial post & coding example; it isnt actually showing two tables but rather it is switching its view from one table to the other after the user makes a selection. Am I correct?

The old DataGrid control had the ability to show records from both tables at the same time. For example the DataGrid would display all of your master records and at the begining of each row would be a expandable plus/minus sign. You could expand each master record and then see listed each of the child records from the second table linked to that record.

The below quote is from the MSDN help file:

The DataGrid control is retained for backward compatibility and for special needs. For nearly all purposes, you should use the DataGridView control.

The only feature that is available in the DataGrid control that is not available in the DataGridView control is the hierarchical display of information from two related tables in a single control.

You must use two DataGridView controls to display information from two tables that are in a master/detail relationship.

Link to Reference(s)
 
How r u Tom
Sorry for the mixup.
Since the only way to display both Master and related tables is in two Datagridview controls, is it possible to display the second Datagridview control
in a second form. If yes which coed would I use.

Thanks Viper
 
The Datagrid control is still available for backwards compatibility, I havent tried it since switching to the newer VS version but it might be easiest to add a reference to the old DataGrid control (without the word VIEW on the end). This will allow multiple tables within it.

To answer your question though, yes you can add a second DataGridView control either to the same form or a new form and have it show child records as they select them in the first control.
 
How r u Tom
Sorry for the mixup.
Since the only way to display both Master and related tables is in two Datagridview controls, is it possible to display the second Datagridview control
in a second form. If yes which coed would I use.

Thanks Viper

Yes, just make the second form have a constructor that takes a BindingSource as a parameter, and pass the bindingsource that is bound to the parent table, remembering to set the DataMember string to the name of the datarelation exposed by the parent BS

VB.NET:
  Class CHildForm
    Public Sub New(bs as BindingSource, relationName as string)
      Me.ChildBindingSource.DataSource = bs
      Me.ChildBindingSource.DataMember = relationName
    End Sub
  End Class


..in parent form:

Dim cf as New ChildForm(Me.ParentBindingSource, "Customers_OrdersDataRelation")
 
Back
Top