467,145 Members | 1,083 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

Synchronize Two Comboboxes

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
Nov 15 '07 #1
  • viewed: 3719
Share:
5 Replies
Please use databinding.

http://www.alvas.net - Audio tools for C# and VB.Net developers
"Anthony Bollinger" <to***@noemail.noemail/
: news:%2****************@TK2MSFTNGP04.phx.gbl...
>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

Nov 16 '07 #2
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Nov 16 '07 #3
See
http://www.emoreau.com/Entries/Artic...-ComboBox.aspx

--
HTH

ric Moreau, MCSD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc. (http://www.emoreau.com)
Membre du rseau .NET Expertise www.dotnet-expertise.com
"Anthony Bollinger" <to***@noemail.noemailwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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

Nov 16 '07 #4
Thanks Linda and Eric. I am attempting it with an Access DB, so I will give
your suggestions a whirl. Great input. --Tony
Nov 16 '07 #5
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

Nov 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Thomas Rademacher | last post: by
4 posts views Thread by gene.ellis@gmail.com | last post: by
1 post views Thread by Craig G | last post: by
1 post views Thread by Marcin Podleny | last post: by
2 posts views Thread by Matt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.