I am building a window form application. I want to populate a combo box based on what the person selects from the first combo box. all my records are stored in a single database table.
Asked
Active
Viewed 315 times
-1
-
4and your question is? – pm100 Nov 15 '16 at 18:00
-
you need an event handler on box one that gets called when its selected item changes. Use that to populate the second combo – pm100 Nov 15 '16 at 18:05
1 Answers
0
Like pm100 suggested, you'll need to be registered to the first combo box's SelectedIndexChanged
event and when it gets fired you'll retrieve the second combo box's values based on the first combo box SelectedText
or SelectedItem
property.
For example, say you'll register to the event on form load event:
cbx1.SelectedIndexChanged += Cbx1_SelectedIndexChanged;
Then when the event gets fired:
private void Cbx1_SelectedIndexChanged(object sender, EventArgs e)
{
cbx2.Items.Clear(); // Clear to add new retreived items
if (cbx1.SelectedIndex != -1)
{
// Retrieve the items based on cbx1's selected item
var items = Repository.RetreiveItems(cbx1.SelectedText);
cbx2.Items.AddRange(items);
}
}
And that is basically it. You'll may want to consider retrieving the items asynchronously so that the UI will be free.

MaorB
- 117
- 2
- 10