How to Use the Same BindingSource Across Multiple UserControls

J

jehugaleahsa

Hello:

I'm working on improving some of our Windows Forms.

I have created two user controls that I want to bind to the same
BindingSource.

I have an overview control that is used just to identify a customer.
Then there is a details view that shows all their specifics. I wanted
to keep navigation outside of my view. There is also a
BindingNavigator that I want to hook up to the same BindingSource.
When I navigate, I want the two user controls to keep in-sync.

The problem is that I have a BindingSource in each control and I bind
all the child controls to it. So, when I try to change the
BindingSource, it appears as though nothing happens. This is because
the child controls are hard-set to point at the original
BindingSource.

Is there a way to change which BindingSource the child controls are
looking at?

Tell me if I'm being unclear.

Thanks,
Travis
 
J

jehugaleahsa

I found a working solution to this problem. I created a class called
IndirectBindingSource. It inherits from and implements all the classes
and interfaces of BindingSource. I implement all of the methods by
redirecting them to the methods inside a BindingSource that I pass via
a property.

The designer just thinks I have another BindingSource. In my user
controls, I still have a BindingSource that points to the data. I set
my IndirectBindingSource's BindingSource to that BindingSource. The
BindingSource picks up all the type properties. My
IndirectBindingSource picks up all of those properties too. I just
bind my user controls to the IndirectBindingSource.

My user controls provide a property called BindingSource that will get
and set the BindingSource inside the IndirectBindingSource. Since
these properties are visible to the Designer, I can set all my
BindingSources at design time.

At run time, I just set the DataSource of the BindingSource in my main
form. Since every control uses the same BindingSource, directly or
indirectly, everything works out smoothly.

It is pretty cool, if not a little hackish. The code is pretty simple
too. Outside of the user controls, no one has any clue I am being
sneaky.
 
K

Ken Foskey

I have to say this is a most confusing description of a problem.

If you want the binding source to follow everything then ALL controls
should bind to it. When you want to split you then have to create a
second binding source. For example, I have two players from one team on
the field at any one time, I have a tab with two binding sources one for
each player and they work independently. If I use one binding source for
all then changing details on one player will change the second to that
player automatically.

I had problems changing position on a binding source this was related to
null datetime elements, the datetime controls reject the update and the
binding source moves you back.
 

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