user1530197
user1530197

Reputation:

Combobox's elements filtering

I have combobox1 and comboox2 and in combobox1 my elements are A,B,C and combobox2 1,2,3,4,5,6...

A related 1,2,3 and B related 3,4 and C related 5,6.

When I choose "A" I want to see just 1,2,3 ; when select "B" just 3,4 etc.

How can I imagine it ?

I tried to do in selected index changed but I didn't

Upvotes: 1

Views: 1008

Answers (1)

Embedd_0913
Embedd_0913

Reputation: 16565

Try this , make a class like this:

public class Data
    {
        public string Name { get; set; }
        public List<int> Values { get; set; }
    }

then in your form have a variable like this:

private List<Data> data = new List<Data>
        {
            new Data{Name="A",Values=new List<int>{1,2,3}},
            new Data{Name="B",Values=new List<int>{4,5}},
            new Data{Name="C",Values=new List<int>{6,7}},
        };

then in the form's constructor :

comboBox1.DisplayMember = "Name";
            comboBox1.DataSource = data;            
            comboBox1.SelectedIndex = 0;

then on the combobox1's selectedindex changed event do like this:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;
            comboBox2.DataSource = data[index].Values;
        }

It should work.

Upvotes: 2

Related Questions