Fill a second listbox with child relations for a first listbox (Simple?)

  • Thread starter Thread starter CrowT
  • Start date Start date
C

CrowT

This should be simple, but I've been stumped for hours. The scenario
involves two tables:

tblAnimals tblAnimalDetail
AnimalID Animal DetailID AnimalID Name Weight
-------- ----------- -------- -------- ------ ------
1000 Dog 100 1000 Rex 20
1001 Cat 101 1000 Spot 16.5
1002 Hamster 102 1000 King 14
103 1001 Fluffy 8
104 1001 Benjy 9.5
105 1002 Furry 1.2

Using VB.Net and ADO.NET (v.2003) I create a form with two listboxes
named lstAnimals and lstDetail. On form Open, a dataset is created.
The two tables shown above are loaded into the dataset individually as
two separate DataTables, and a DataRelation is created between them on
[AnimalID]. So far so good -- I accomplished these steps successfully.

Listbox 'lstAnimals' displays the values in the first table. I
accomplished this successfully as follows:
Me.lstAnimals.DisplayMember = "Animal"
Me.lstAnimals.ValueMember = "AnimalID"
Me.lstAnimals.DataSource = ds.Tables("tblAnimals")

Now, I'd like listbox 'lstDetail' to display the list of corresponding
[Name] values in 'tblAnimalDetails' when one of the values in the
first listbox is clicked (eg, click on "Cat" in the first listbox and
you'll see "Benjy" and "Fluffy" in the second). This is where I'm
stuck. How do I set up the second listbox to do this??? Setting the
DataSource to myDataSet.tables("tblAnimalDetail") returns all animals
all the time, while setting the DataSource to the DataRelationship
returns an error (ie, me.lstDetail.DataSource =
myDataSet.Relations("myRelationshipName").

Many thanks in advance for your input.
 
You aren't that far off ... first of all, you WANT your second listbox to
contain all the animal Names "all the time". That is how you will select
them.

When you select the animal in the first listbox, you will pass the AnimalID
to the second listbox as the SelectedIndex. In the second listbox, you will
bind the AnimalID as the ValueMember and the Name as the display member. I
don't even see the need for the data relation, because your datasource for
the second listbox is tblAnimalDetail.
 
Back
Top