How do I share a BindingSource between Winforms.

How do I share a BindingSource between Winforms.

Post by Rob Do » Sat, 23 Sep 2006 05:18:16



Hi,

I have two forms within a c# winform app, one form contains a datagridview
bound to a binding source, the other form contains a detailed view of that
same binding source. what I am trying to do is pass the binding source from
form one to form two within the contructor, as shown below, the problem is
that the moment I assign the binding source passed from form one to that of
form two, all the databound controls in form two are empty instead of
containing the bindingsource current rows data. I have tried using the
BindingSource.ResetBindings(), but it does nothing.
public frmCustomersDetail(BindingSource oBindingSource)
{
InitializeComponent();
CustomersBindingSource = oBindingSource;
}
 
 
 

How do I share a BindingSource between Winforms.

Post by Joanna Car » Sat, 23 Sep 2006 05:29:50


XXXX@XXXXX.COM ...

| I have two forms within a c# winform app, one form contains a datagridview
| bound to a binding source, the other form contains a detailed view of that
| same binding source. what I am trying to do is pass the binding source
from
| form one to form two within the contructor, as shown below, the problem
is
| that the moment I assign the binding source passed from form one to that
of
| form two, all the databound controls in form two are empty instead of
| containing the bindingsource current rows data. I have tried using the
| BindingSource.ResetBindings(), but it does nothing.
| public frmCustomersDetail(BindingSource oBindingSource)
| {
| InitializeComponent();
| CustomersBindingSource = oBindingSource;
| }

Why not put one BindingSource on each form and connect their DAtaSource
properties to the same underlying list or dataset ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

 
 
 

How do I share a BindingSource between Winforms.

Post by Rob Do » Sat, 23 Sep 2006 05:54:16

> Why not put one BindingSource on each form and connect their DAtaSource


you mean like this? ( I tried that and it seems to work okay, until I
select a record from the first form after sorting by column and then it
grabs the wrong record )

public frmCustomersDetail( DataSetOrders oDataSet)
{
InitializeComponent();
this.dataSetOrders = (DataSetOrders)oDataSet;
CustomersBindingSource.DataSource = (DataSetOrders)oDataSet;
CustomersBindingSource.Position = oBindingSource.Position;
}
 
 
 

How do I share a BindingSource between Winforms.

Post by Bart Mermu » Sat, 23 Sep 2006 18:40:15

Hi,




You have to make a difference between an instance and a variable.

You have put a BindingSource on the Form which you have bound. So inside
InitializeComponent() an instance of BindingSource is created, it's assigned
to CustomersBindingSource and it's bound to various Controls.

Then you change the variable CustomersBindingSource to oBindingSource but
the Control's aren't bound to a _variable_ but to an _instance_ (set inside
InitializeComponent()). So they remain bound to the same instance.

So it's definitely not a bug, just the normal way instances and variables
work. But you could say it's a limitation of the designer (databinding) in
that it can't refer to instances on other Forms. (in code this isn't a
problem)

HTH,
greetings
 
 
 

How do I share a BindingSource between Winforms.

Post by Rob Do » Sat, 23 Sep 2006 20:31:32

Hi,

I understand what your saying, but to have to manually rebind controls in
order to be able to pass the Bindingosurce is CRAZY, can't you assign just
the address. to that instamce as you would in c++ or does c# not support
this.. I sure can't see myself retyping all those databindings..

Thanks,
 
 
 

How do I share a BindingSource between Winforms.

Post by Rob Do » Sat, 23 Sep 2006 20:39:07

Why couldn't CustomersBindingSource be *CustomersBindingSource and then we
could just assign the address of oBindingSource as in
*CustomersBindingSource = &oBindingSource;

thanks,
 
 
 

How do I share a BindingSource between Winforms.

Post by Bart Mermu » Sun, 24 Sep 2006 01:06:44

i,

"Rob Dob" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

Consider ( C# ) :

BindingSource A = new BindingSource();
// A is a reference to the new BindingSource

BindingSource B = A;
// B is now too a reference to that BindingSource (as in Binding storing the
datasource in it's own variable)

A = new BindingSource();
// A is now a reference to another BindingSource, but B didn't change


Since BindingSource is a reference type, this would be "similar" to the
following in C++ :
BindingSource* A = new BindingSource();
BindingSource* B = A;
A = new BindingSource(); // B didn't change

Though something like this is possible in C++:
BindingSource* A = new BindingSource();
BindingSource** B = &A;
A = new BindingSource(); // *B also changed

But that's C++ not C# and if A would be a member variable instead of a local
one then it would be more complicated (see c++ pointer-to-member) so i don't
believe this would solve this problem.

The problem is you can't change InitializedComponent where the BindingSource
is created and bound, it doesn't need to create one(at runtime), you already
have one.

So i'm still considering this a designer shortcoming and currently have no
solution other than (re)binding in code... you could automate this, by
looping through all controls and databindings, removing bindings and adding
new ones with a different DataSource, pretty ugly too.


HTH,
Greetings