Oktam Yaqubov
Oktam Yaqubov

Reputation: 113

Create relationship between two ComboBoxes

I have two tables (categories and sub categories) that are related. So I have 2 ComboBox controls on a Windows forms. Those 2 ComboBox are related in a parent-child (or category-sub_category) relationship. For example, i have a ComboBox that contains a list of categories for the user to choose from (the parent) and another ComboBox (the child) that contains the list of sub categories. Now I need that: if user select category from first combobox then at the second combobox must appear sub categories that related to the category. For example:

| Category
| cat_id   | cat_name |
| 1        | Car      |
| 2        | Car1     |
| 3        | Car2     |
| 4        | Car3     |

And sub categories

| SubCategory
| scat_id   | scat_name  | cat_id |
| 1         | sCar       | 1      |
| 2         | sCar1      | 1      |
| 3         | sCar2      | 3      |
| 4         | sCar3      | 1      |

These are two related of structure in table. And I have this C# code:

private void SInfo_Load(object sender, EventArgs e)
{
    using (var context = new StBaseSQLEntities())
    {
        metroComboBox1.DataSource = context.Category.ToList();
        metroComboBox1.DisplayMember = "cat_name";
        metroComboBox1.ValueMember = "cat_id";

        //SubCategory
        metroComboBox2.DataSource = context.SubCategory.ToList();
        metroComboBox2.DisplayMember = "scat_name";
        metroComboBox2.ValueMember = "scat_id";
    }
}

I'm new in C# Windows Forms, so I do not find how to do this. If I select 1 from the category combobox, then I second combobox need to show sub-categories that belong to 1st id in the sub category comboboxs. How can I get the result in C# win forms?

Upvotes: 1

Views: 706

Answers (2)

Fabio
Fabio

Reputation: 32445

... if user select category from first combobox then at the second combobox must appear sub categories that related to the category.

Use ComboBox.SelectionChangeCommitted Event
From docs:

Occurs when the user changes the selected item and that change is displayed in the ComboBox

Other "change" events occurs even when selected value were changed programmatically.

Save all subdirectories as private member, so you will be able to filter it without reading database.

private List<SubCategory> _allSubCategories;

private void SInfo_Load(object sender, EventArgs e)
{
    using (var context = new StBaseSQLEntities())
    {
        metroComboBox1.DataSource = context.Category.ToList();
        metroComboBox1.DisplayMember = "cat_name";
        metroComboBox1.ValueMember = "cat_id";

        //SubCategory
        _allSubCategories = context.SubCategory.ToList();
        metroComboBox2.DataSource = _allSubCategories;
        metroComboBox2.DisplayMember = "scat_name";
        metroComboBox2.ValueMember = "scat_id";
    }
}

Then in SelectionChangeCommitted eventhandler

private void metroComboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    var combobox = (ComboBox)sender;
    var selectedCategory = (short)combobox.SelectedValue;
    metroComboBox.DataSource = 
        _allSubCategories.Where(sub => sub.cat_id == selectedCategory).ToList();
    // display/enable item
}

Upvotes: 0

Davide Vitali
Davide Vitali

Reputation: 1035

Just use the SelectedValue property on first combobox as a filter for the subcategory:

private void MetroComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    ComboBox cmb = (ComboBox) sender;
    MetroComboBox2.DataSource = 
                      context.Subcategory.Where(x => x.cat_id == cmb.SelectedValue).ToList();
    MetroComboBox2.enabled = true;
}

Upvotes: 1

Related Questions