Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Share BindingSource between two forms
 

Share BindingSource between two forms

Hi, i have two forms and im having problems sharing the binding source between the two.

I am using custom business objects, and in the designer set form one's binding source's data source to my business object type in the data sources window. The designer then places the code in the form:

m_BindingSource.DataSource = typeof(MyObject);

I then can setup my DataGridView.DataSource = m_BindingSource;

In my constructor i would then set the m_BindingSource.Datasource = new BindingList<MyObject>();

Everything all works well. Then i would make a second form, and create a second binding source as before so i can select which fields i want to show on that form (for arguments sake lets say i am making a details form). I then set the control bindings up as per the first form, and set a property on that form which allows me to set the binding source called BindingSource.

So, from my form1 constructor,

Form1()
{
InitializeComponent();
m_BindingSource.Datasource = new BindingList<MyObject>();
Form2 form2 = new Form2();
form2.BindingSource = BindingSource;
form2.Show();
}

The trouble is i can tell the two forms arent linked as nothing updates when i navigate through the dataset from form1!. One thing i have found is if i set the BindingSource in Form2's BindingSource property, then do manual binding by hand, it works? It is almost like i need to force all controls to rebind.

Any ideas? I would like to stick with using the designer, and be able to link binding sources at will, i dont want to hand code!

Thanks in advance,
Chris

ChrisMorley  Friday, March 03, 2006 9:00 PM
mt3ch  Thursday, May 18, 2006 12:10 PM
Hi,

I have the same problem. The way I understand then resetbindings should do the job, but it does not work.

Do you have any further ideas?

regards
Stefan
Stefan K.  Wednesday, April 22, 2009 10:13 AM
I'm having the same problem.

My diagnostic is that this line:

form2.BindingSource = BindingSource;

effectively unbinds all the controls of the second form (technically, they are still bound, but to the previously assigned BindingSource, which is not referenced by form2.BindingSource anymore).

I got a little further by doing this:

form2.BindingSource.DataSource = BindingSource;

instead. My problem now is that I want my second form to be able to reassign an object in the bound list of objects. Unfortunately, I cannot access the initial bindingsource in my second form. Should I have a second reference to the BindingSource in my form2?

Is there a known technique for this kind of scenario?
Billy Bob Richert  Tuesday, August 04, 2009 7:25 PM

You can use google to search for other answers

Custom Search

More Threads

• problem at datagridview
• Mouse clicking not fired OnKeyDown even
• cancel DataGridView RowLeave Event?
• VB2005 Set focus to cell in new row in datagridview
• Printing Multiple Pages
• Get structure of DataGridView from the DataSource at runtime
• Change the color of the SortGlyph
• Filling data from SQL 2005 in ASP.NET
• Xml file to dataset , content to listbox and combobox
• Rotating rows and columns in DataGridView 2.0