Question

Goal

I want to be able to have two ComboBoxes where one is the parent or owner of the second one. This means that whenever I select a value in the first ComboBox, the second ComboBox will filter it's results to display the corresponding values related to the first ComboBox.

For example:

ComoBox Filtering Example

Note: This example has been completed programmatically... I want to figure out how to do it using the user interface of Visual Studio


Current Situation

I have a dataset with two DataTables like so:

DataSet

As you can see, I have a parameter in my BakerySubSectionsTableAdapter called @FK_BakerySection. I want to link that to the BakerySection's PK_BakerySection attribute.


Current Result

Here is my current result:

Current Result

Using the following query in my TableAdapter:

Query Builder

So ... How do we set a value to a parameter using the User Interface?

Was it helpful?

Solution

It's easy if you add a DataRelation between your two tables (IIRC you can simply do this in the DataSet designer).

Then you only have to set the DisplayMember of your second ComboBox to ParentTable.NameOfRelation.NameToDisplay.


Here's a small, complete example:

enter image description here


Dim data = New DataSet()
Dim section = data.Tables.Add("Section")
section.Columns.Add("ID", GetType(Integer))
section.Columns.Add("Name", GetType(String))

Dim sub_section = data.Tables.Add("SubSection")
sub_section.Columns.Add("ID", GetType(Integer))
sub_section.Columns.Add("Name", GetType(String))
sub_section.Columns.Add("Section", GetType(Integer))

section.Rows.Add(New Object() {1, "Foo"})
section.Rows.Add(New Object() {2, "Bar"})

sub_section.Rows.Add(New Object() {1, "Sub Foo", 1})
sub_section.Rows.Add(New Object() {2, "Another Sub Foo", 1})

sub_section.Rows.Add(New Object() {3, "Sub Bar", 2})
sub_section.Rows.Add(New Object() {4, "bar bar bar", 2})
sub_section.Rows.Add(New Object() {5, "more bar", 2})

section.ChildRelations.Add("SectionToSub", section.Columns("ID"), sub_section.Columns("Section"))

Dim f = New Form()
Dim c1 = New ComboBox() With { _
    .DataSource = data, _
    .DisplayMember = "Section.Name", _
    .ValueMember = "Id" _
}
Dim c2 = New ComboBox() With { _
    .DataSource = data, _
    .DisplayMember = "Section.SectionToSub.Name", _
    .ValueMember = "Id" _
}
Dim fl = New FlowLayoutPanel()
fl.Controls.Add(c1)
fl.Controls.Add(c2)
f.Controls.Add(fl)
f.ShowDialog()

Just make sure your BakerySubSections is completly filled (no need for the parameter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top