Bind DataView to 2 ComboBoxes

J

Jazper Manto

hi

i bounded the same dataview to 2 different comboboxes. now, when i change
the selectedIndex of combobox1, it automatically changes the selectedIndex
of combobox2.
does this mean, that the combobox selects the item on the base of some kind
of a pointer on the dataview? if yes, is there a class inside the framework,
which i can use to change this pointer on the dataview myself?

thanx for any hint.
jazper
 
B

Bart Mermuys

Hi,

Jazper Manto said:
hi

i bounded the same dataview to 2 different comboboxes. now, when i change
the selectedIndex of combobox1, it automatically changes the selectedIndex
of combobox2.
does this mean, that the combobox selects the item on the base of some
kind
of a pointer on the dataview?

Well, there is a little more to it. The DataView itself doesn't mantain the
position, but a CurrencyManager does. A CurrencyManager is unique for each
(bound) DataSource/DataMember within a BindingContext. By default a
BindingContext is shared between a Form and all of its controls.

If you want independent navigating with the same DataSource/DataMember, you
need to assign a new BindingContext to the different controls as this would
result in a different CurrencyManager (for the same DataSource/DataMember).
if yes, is there a class inside the framework,
which i can use to change this pointer on the dataview myself?

The general way the get a CurrencyManager would be:
CurrencyManager cm = (CurrencyManager)BindingContext[dataSource,
dataMember];

But if the DataMember wasn't used for binding then it can be omitted, eg. :
CurrencyManager cm = (CurrencyManager)BindingContext[myDataView];

Lookup the CurrencyManager class, it has a few properties, like Position
which you can read and change.


HTH,
Greetings
 
J

Jazper Manto

Well, there is a little more to it. The DataView itself doesn't mantain
the
position, but a CurrencyManager does. A CurrencyManager is unique for each
(bound) DataSource/DataMember within a BindingContext. By default a
BindingContext is shared between a Form and all of its controls.

perfect answer. thank you.
you said the BindingContext is shared between a Form and all of its
controls... hmmm. i have another problem which has been explained while i
read your statement. i have a winform with a panel-object in it. this panel
contains bounded comboboxes. as soon as i grap this panel and add it to
another controlcollection of another form it gets another CurrencyManager.

can i avoid this problem or set back the CurrencyManager in some way?

thanx jazper
 
B

Bart Mermuys

Hi,
[Inline]

Jazper Manto said:
perfect answer. thank you.
you said the BindingContext is shared between a Form and all of its
controls...

Yes, to explain this a little more, the Form creates a BindingContext, then
when you bind to any Control it needs a BindingContext but if none have been
set to that Control it will use the BindingContext of the parent.

On the other hand, if a BindingContext has been explicitly set to a Control
it will no longer use its parents one, not even when you move the Control
between forms.
hmmm. i have another problem which has been explained while i
read your statement. i have a winform with a panel-object in it. this
panel
contains bounded comboboxes. as soon as i grap this panel and add it to
another controlcollection of another form it gets another CurrencyManager.

can i avoid this problem or set back the CurrencyManager in some way?

Yes, by keeping the same BindingContext.

In Form load (preferably before binding, but it's not mandatory) explicitly
set the panel's BindingContext. Like explained above, once you have set it
explicitly it won't change when you add the panel to another Form.

You still have a choice between just using the parent's BindingContext or
creating a new one, eg:

panel1.BindingContext = new BindingContext();
---or---
panel1.BindingContext = this.BindingContext();

The latter may look useless but it isn't because here you're setting it
explicitly which makes the difference.

HTH,
Greetings
 
J

Jazper Manto

hi bart

again thank you for your explanations.
Yes, to explain this a little more, the Form creates a BindingContext, then
when you bind to any Control it needs a BindingContext but if none have been
set to that Control it will use the BindingContext of the parent.

On the other hand, if a BindingContext has been explicitly set to a Control
it will no longer use its parents one, not even when you move the Control
between forms.

perfect. excactly the solution i needed. :)
cheers, jazper
 

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