Textbox as parent and DataGridView as child

janilane

Active member
Joined
Jan 23, 2008
Messages
30
Programming Experience
Beginner
Hi,
I was able to do a data relation between parent and child, and using a bindingcontextbase I was able to display in textbox and navigate the records of the parent table. How can I make the datagridview display the child records when the textbox value has changed when I navigate it by not using a SQL command? I've googled every site but I still can't figure out how to do this. Thanks
sample code:

Dim sqlAdapter As SqlClient.SqlDataAdapter = Nothing
sqlAdapter = New SqlClient.SqlDataAdapter()
sqlAdapter.SelectCommand = New SqlCommand("select * from zCT_Contact", sqlCon)


Dim dSet As New DataSet("All Tables")
sqlAdapter.Fill(dSet, "NewTable 1")
sqlAdapter = New SqlClient.SqlDataAdapter()
sqlAdapter.SelectCommand = New SqlCommand("select * from zAD_Address", sqlCon)
sqlAdapter.Fill(dSet, "NewTable 2")

sqlAdapter = New SqlClient.SqlDataAdapter()
sqlAdapter.SelectCommand = New SqlCommand("select * from ar_category", sqlCon)
sqlAdapter.Fill(dSet, "NewTable 3")

TextBox1.DataBindings.Add(New Binding("Text", dSet, "NewTable 1.Name"))
TextBox2.DataBindings.Add(New Binding("Text", dSet, "NewTable 1.Address"))

'create the binding manager for each table
bMgrBase = Me.BindingContext(dSet, "NewTable 1")
bMgrBase1 = Me.BindingContext(dSet, "NewTable 2")

'Using DataRelation object
Dim drMyRelation As New DataRelation("dSet", dSet.Tables("NewTable 1").Columns("ctContactID"), dSet.Tables("NewTable 2").Columns("ctContactID"))

dSet.Relations.Add(drMyRelation)

'Code for navigating, sample only...
Private Sub btnFirstRec_Click(...)
bMgrBase.Position = 0
bMgrBase1.Position = 0
End Sub

Private Sub btnNextRec_Click(...)
If bMgrBase.Position < bMgrBase.Count Then
bMgrBase.Position += 1
End If

If bMgrBase1.Position < bMgrBase1.Count Then
bMgrBase1.Position += 1
End If

End Sub

Thanks for all your help.
 
Last edited:
might help someone looking...

with little bit of tweaking,this can be used as parent-child output,got this from another website,at least we now have the idea how to do it...;)

Dim dr, dr_related As DataRow
Dim sb As New System.Text.StringBuilder

' create a relationship between two of the tables
' arguments are Name, ParentCol, ChildCol, create_foreign_key
ds.Relations.Add(New DataRelation("CategoryProducts", _
ds.Tables("Categories").Columns("CategoryID"), _
ds.Tables("Products").Columns("CategoryID"), False))

' list rows from the Child table
For Each dr In ds.Tables("Products").Rows
sb.Append("ProductName='" & dr("ProductName") & "'")
' now get the CategoryName column from the related row
' GetParentRow will only return one table (or nothing if there is no match)
dr_related = dr.GetParentRow("CategoryProducts")
sb.Append(", Category='" & dr_related("CategoryName") & "'" & vbCrLf)
Next

' display our data in a textbox
TextBox1.Text = sb.ToString
' list rows from the Parent table
For Each dr In ds.Tables("Categories").Rows
sb.Append("CategoryName='" & dr("CategoryName") & "'" & vbCrLf)
' now get the CategoryName column from the related row
' GetChildRows will return many rows (or nothing if there is no match)
For Each dr_related In dr.GetChildRows("CategoryProducts")
sb.Append(vbTab & "ProductName='" & dr_related("ProductName") & "'" & vbCrLf)
Next
Next

' display our data in a textbox
TextBox2.Text = sb.ToString
 
You'll struggle because a textbox is meant for editing NOT for nav.. I'd additionally go as far to say as that code you got from the other site is the biggest mess of a perfect example how NOT to do this, that I would recommend you avoid utilising further advice from that site; they dont seem to be very awake and up to date because that advice is definitely from 2003 or earlier (.net 1.1)

The best advice I could give as regards achieving this would be to have:

parent bindindingsource
child bindingsource
dgv bound to child bs
child bs bound to datarelation of parent bs
call:

parentBS.Position = parentBS.Find("column_name", txtSearchFor.Text)

and the child will show the related records


Yep, that's right, if that other site could have only given you sensible modern advice you could have set this up with about 10 mouse clicks and one line of code instead of that gash mess you've got there ;)
 
Back
Top