Combo Box - Data and Key

R

rlm

This small problem to solve involves a two table database (one parent
and one child table), one winform and two combo boxes.

Combo box A is populated in the Load event. Combo box A pulls data from
the parent table. A second combo box (box B) will be populated (from
the child table) based on data selected in by the user in combo box A.

In the datastore the child contains a numeric foreign key from the
parent.

The goal is to figure out how to display user friendly data in combo
box a and maintain the associated key so that when the user makes a
selection in box a the approriate data is displayed in combo box b
based on the key from the parent.

I believe this is a very common situation.

Microsoft has helped me figure out that the SelectedIndexChanged is the
event where I want to place my code. Note also that I am not looking
for info on how to retrieve data from a database. I have that info.

As an example here is sample data:

Data to display associated key
--------------------- ---------------------
Test 1 1
Test 2 3
Test 4 5

When user selects data in combo box a I need to produce the query
SELECT field1, field2, fieldx FROM childTable WHERE parentKey =
[associated key].

Where do I store the associated key in the combo box? How do I retrieve
the same?

I am looking at the ValueMember property? However, when I attempt to
interrogate the combo box in the dev UI I am seeing "cannot view
indexed property". Morever, I do not see a index on ValueMember.

Thanks in advance.
 
M

Matt Bailey

When you populate the combo box, you can set the text to be displayed
as well as a value to be associated with the data

E.g.
comboBox.Items.Add(new ListItem("text", value));


You can put the key into the "value" field and the display text into
the "text" field

Hope that helps.
 

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