Multiple ComboBoxes, one data source

M

Matt

Hi all, me again! :)

I've now got an issue with combo boxes. Basically, I have a number of
items that I want a user to pick from a single list. It's basically
along the lines of:

Fruit 1: [value]
Fruit 2: [value]
Fruit 3: [value]

the values are setup in a string array called fruitList[]

I have set the datasource on all the comboBoxes to be fruitList[]
however when I change the selected item in on comboBox, it changes in
all the others as well!

Is there a way that I can use one array across multiple comboBoxes
without having it change across the boxes every time? An example is
as follows:

Fruit 1: [Apple]
Fruit 2: [Apple]
Fruit 3: [Apple]

I want to make fruit 1 an apple, fruit 2 a bannana and fruit 3 a pear,
however when I select bannana for fruit two, I get the following:

Fruit 1: [Bannana]
Fruit 2: [Bannana]
Fruit 3: [Bannana]

The comboBoxes are named comboBox1 to comboBox8 (lazy I know, but this
is just a proof of concept at the moment!)

All help is welcomed!
 
N

Nicholas Paldino [.NET/C# MVP]

Matt,

This is simple. You can bind all the combo boxes to the array, you just
have to make sure that each combobox uses a separate BindingContext.

So, for your comboboxes, you want to do:

comboBox1.BindingContext = new BindingContext();
comboBox2.BindingContext = new BindingContext();
comboBox3.BindingContext = new BindingContext();

When they are created, they inherit from the control that is hosting
them. Since you didn't specify it, it are all using the context of the
control/form that they are hosted in, and that's why when you change one
combo box, they all change.

You could also get around this by copying the contents of the array into
a new array for each combobox. This will cause the binding context to look
at the data as separate data sources, and they will have their own binding
context.

Hope this helps.
 
M

Matt

Matt,

This is simple. You can bind all the combo boxes to the array, you just
have to make sure that each combobox uses a separate BindingContext.

So, for your comboboxes, you want to do:

comboBox1.BindingContext = new BindingContext();
comboBox2.BindingContext = new BindingContext();
comboBox3.BindingContext = new BindingContext();

When they are created, they inherit from the control that is hosting
them. Since you didn't specify it, it are all using the context of the
control/form that they are hosted in, and that's why when you change one
combo box, they all change.

You could also get around this by copying the contents of the array into
a new array for each combobox. This will cause the binding context to look
at the data as separate data sources, and they will have their own binding
context.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all, me again! :)
I've now got an issue with combo boxes. Basically, I have a number of
items that I want a user to pick from a single list. It's basically
along the lines of:
Fruit 1: [value]
Fruit 2: [value]
Fruit 3: [value]
the values are setup in a string array called fruitList[]
I have set the datasource on all the comboBoxes to be fruitList[]
however when I change the selected item in on comboBox, it changes in
all the others as well!
Is there a way that I can use one array across multiple comboBoxes
without having it change across the boxes every time? An example is
as follows:
Fruit 1: [Apple]
Fruit 2: [Apple]
Fruit 3: [Apple]
I want to make fruit 1 an apple, fruit 2 a bannana and fruit 3 a pear,
however when I select bannana for fruit two, I get the following:
Fruit 1: [Bannana]
Fruit 2: [Bannana]
Fruit 3: [Bannana]
The comboBoxes are named comboBox1 to comboBox8 (lazy I know, but this
is just a proof of concept at the moment!)
All help is welcomed!

Nicholas,

That worked a treat! Thanks!

M.
 

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