472,353 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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
5 3949
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 réseau .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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Thomas Rademacher | last post by:
Hallo, I want to synchronize the main function of my script. I havn't a class structure in my script. def main(): # my code to synchronized ...
4
by: gene.ellis | last post by:
Using a web interface, I am placing text into a SQL database. From time to time, I would like to synchronize one of my other tables in the database...
4
by: Kathy | last post by:
In my custom menu one menu item opens a form to add new Makes to TblMakes and another menu item opens a form to add new Models to TblModels. I have...
0
by: johnson_cy | last post by:
I am using Access 2000. My main form has a record source of a table (employeeTbl with key of . It is filtering the record results. My subform...
8
by: Zlatko Matiæ | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox....
1
by: Craig G | last post by:
for months we have been dabling round looking at various 3rd party comboboxes that allow the user to type text into the combobox as a search...
1
by: Marcin Podle¶ny | last post by:
Hi, I've a form with three comboboxes. Each of them contains the same data - all cities in my country. As you see it's a lot of data so it takes...
2
by: Matt | last post by:
Hi all, me again! :) I've now got an issue with combo boxes. Basically, I have a number of items that I want a user to pick from a single list. ...
2
by: Wingot | last post by:
Hey, I have a view to a database that I have created for Client Maintenance. It has a number of fields, but the important ones are Medical...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.