468,133 Members | 1,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Multiple ComboBoxes, one data source

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!

May 4 '07 #1
2 10995
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]
- mv*@spam.guard.caspershouse.com

"Matt" <ma***********************@fujifilmsericol.comwrot e in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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!

May 4 '07 #2
On 4 May, 20:11, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"Matt" <matthew.macdonald-wall...@fujifilmsericol.comwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...
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.

May 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by TheMadHatter | last post: by
5 posts views Thread by Anthony Bollinger | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.