Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / September 2006

Tip: Looking for answers? Try searching our database.

How do I share a BindingSource between Winforms.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob Dob - 21 Sep 2006 21:18 GMT
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;
}
Joanna Carter [TeamB] - 21 Sep 2006 21:29 GMT
| 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
[quoted text clipped - 9 lines]
| CustomersBindingSource = oBindingSource;
| }

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

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

Rob Dob - 21 Sep 2006 21:54 GMT
> Why not put one BindingSource on each form and connect their DAtaSource
> properties to the same underlying list or dataset ?

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;
}
Bart Mermuys - 22 Sep 2006 10:40 GMT
Hi,

> Hi,
>
[quoted text clipped - 11 lines]
> CustomersBindingSource = oBindingSource;
> }

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
Rob Dob - 22 Sep 2006 12:31 GMT
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,

>> public frmCustomersDetail(BindingSource oBindingSource)
>> {
[quoted text clipped - 19 lines]
> HTH,
> greetings
Rob Dob - 22 Sep 2006 12:39 GMT
Why couldn't CustomersBindingSource be *CustomersBindingSource  and then we
could just assign the address of oBindingSource as in
*CustomersBindingSource = &oBindingSource;

thanks,

> Hi,
>
[quoted text clipped - 32 lines]
> HTH,
> greetings
Bart Mermuys - 22 Sep 2006 17:06 GMT
Hi,

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

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

> thanks,
>
[quoted text clipped - 35 lines]
>> HTH,
>> greetings

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.