datatables: bind child relation table to a datagridview

DustyCarpet

Member
Joined
Sep 4, 2013
Messages
8
Programming Experience
1-3
Hi

I have created a parent/child data relation between 2 datatables with the following code.
But I can't figure out how to bind the child table to a datagridview.

Any ideas.

thanks



VB.NET:
   Dim data_relation As New  _
            DataRelation("DataTableJoin", _
            ds.Tables("dtParent").Columns("ID"), _
 _
            ds.Tables("dtChild").Columns("ID"))
        ds.Relations.Add(data_relation)
 
To bind anything to a TextBox you do this:
myTextBox.DataBindings.Add("Text", myDataSource, "SourceColumnName")
In your case, the data source is the BindingSource.
 
Thanks, I tried this already, binding the textbox to the bindingsource and I get the error. " Cannot bind to the property or column BRCID on the datasource. I don't think its because the BRCID column on the datasource is an integer is it?
my code is below:


' fill datatables
daDemographicDetails.Fill(dsQuestionaire, "dtDemography")
daMedHistory.Fill(dsQuestionaire, "dtMedHistory")




' Add a relationship to the DataSet
Dim data_relation As New _
DataRelation("Join", _
dsQuestionaire.Tables("dtDemography").Columns("BRCID"), _
_
dsQuestionaire.Tables("dtMedHistory").Columns("BRCID"))
dsQuestionaire.Relations.Add(data_relation)




' Bind the parent data connector to the table.
masterBindingSource.DataSource = dsQuestionaire
masterBindingSource.DataMember = "dtDemography"


' Bind the child data connector to the master data connector,
detailsBindingSource.DataSource = masterBindingSource
detailsBindingSource.DataMember = "Join"


' Bind child source to datagridview
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = detailsBindingSource


' Bind master source ID column to text box
txtBRCID.DataBindings.Add("Text", masterBindingSource, "BRCID")
 
I think it's because you have bound the BindingSource to the DataSet rather than the DataTable. Try qualifying the column name with the table name, e.g. "MyTable.MyColumn".
 
No, its fixed now. You know what it was, i was trying to bind the masterbindingsource to the textbox before I had bound the masterbindingsource to the dataset. Its my own fault, you couldn't have known that. Thanks so much for your help though.....
 
Back
Top