By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,206 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,206 IT Pros & Developers. It's quick & easy.

Combo Box - Data and Key

P: n/a
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

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.

May 17 '06 #1
Share this Question
Share on Google+
3 Replies

P: n/a
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

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.

May 17 '06 #2

P: n/a
Sorry, as for retrieval...

int key = comboBox.SelectedItem.Value;

May 17 '06 #3

P: n/a
Thank you!

May 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.