Synchronize Two Comboboxes

A

Anthony Bollinger

I have two tables in a master-detail relationship. When I make a selection
in one combobox, how do I have it display the values from a second combobox?
Each table has a key and a text value for each record. I don't yet see the
way to update the list of values in the second combobox.

Thanks for any input,
Tony
 
L

Linda Liu[MSFT]

Hi Tony,

Based on my understanding, you have two DataTables in a master-detail
relationship and two ComboBoxes. What you want is to have one ComboBox show
the data in the master table and the other ComboBox show the corresponding
records in the detail table. If I'm off base, please feel free to let me
know.

If you're using VS05, you can add a DataRelation between the master and
detail tables and use BindingSource as the data source of the ComboBoxes.

I will illustrate this with an example. Create a WinForm application
project and add two ComboBoxes on the form. In the Form Load event hanlder,
add the following code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
Dim parent As New DataTable("ParentTable")
Dim col As New DataColumn("ID", GetType(Integer))
parent.Columns.Add(col)
col = New DataColumn("Text", GetType(String))
parent.Columns.Add(col)

Dim child As New DataTable("ChildTable")
col = New DataColumn("ID", GetType(Integer))
child.Columns.Add(col)
col = New DataColumn("PID", GetType(Integer))
child.Columns.Add(col)
col = New DataColumn("Text", GetType(String))
child.Columns.Add(col)

Dim row As DataRow = parent.NewRow
row(0) = 1
row(1) = "parent text 1"
parent.Rows.Add(row)

row = parent.NewRow
row(0) = 2
row(1) = "parent text 2"
parent.Rows.Add(row)

row = parent.NewRow
row(0) = 3
row(1) = "parent text 3"
parent.Rows.Add(row)

row = child.NewRow()
row(0) = 1
row(1) = 1
row(2) = "child text 1"
child.Rows.Add(row)

row = child.NewRow()
row(0) = 2
row(1) = 1
row(2) = "child text 2"
child.Rows.Add(row)

row = child.NewRow()
row(0) = 3
row(1) = 2
row(2) = "child text 3"
child.Rows.Add(row)

row = child.NewRow()
row(0) = 4
row(1) = 2
row(2) = "child text 4"
child.Rows.Add(row)

row = child.NewRow()
row(0) = 5
row(1) = 3
row(2) = "child text 5"
child.Rows.Add(row)

row = child.NewRow()
row(0) = 6
row(1) = 3
row(2) = "child text 6"
child.Rows.Add(row)

ds.Tables.Add(parent)
ds.Tables.Add(child)
Dim relation As New DataRelation("parent_child",
parent.Columns("ID"), child.Columns("PID"))
ds.Relations.Add(relation)

Dim parentBS As New BindingSource()
parentBS.DataSource = ds
parentBS.DataMember = "ParentTable"

Dim childBS As New BindingSource
' Set the DataSource property of the childBS to the parentBS and
the DataMember property to the data relation. This is the key point.
childBS.DataSource = parentBS
childBS.DataMember = "parent_child"

Me.comboBox1.DataSource = parentBS
Me.comboBox1.DisplayMember = "Text"
Me.comboBox1.ValueMember = "ID"

Me.comboBox2.DataSource = childBS
Me.comboBox2.DisplayMember = "Text"
Me.comboBox2.ValueMember = "ID"

End Sub

Build the project and run the application. You should see the text
displayed in the comboBox1 is "parent text 1" and its drop down list
contains three items "parent text 1", "parent text 2" and "parent text 3".
The drop down list of the comBox2 contains two items "child text 1" and
"child text 2". If you select "parent text 2" in the comboBox1, the drop
down list of the comboBox2 contains "child text 3" and "child text 4".

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Anthony Bollinger

Thanks Linda and Eric. I am attempting it with an Access DB, so I will give
your suggestions a whirl. Great input. --Tony
 
L

Linda Liu[MSFT]

Hi Tony,

I am reviewing this post and would like to know the status of this issue.

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top