Gentlemen
I have a user-control which hosts many other data bound controls. I intend to re-sue this user-control throughout my application.
The hole purpose of this post is to ask if anyone has experience in re-binding controls in a user-control to another BindingSource.
The purpose of the user-control is to select a customer from a list, and then select a contact associated with that customer.
The Customer business object contains a list of contacts.
This information is then stored in an invoice header: the customer and the selected contact.
Within the user-control I have the following binding sources.
bindingSourcCustomers : The source list of customers - bound at design time to typeof(Customer)
bindingsourceContacts : List of contacts - bound to bindingSourceCustomers with the data member set to the Contacts list
bindingSourceInvoiceHeader : The invoice header we wish to store the customer and contact into - bound at design time to typeof(invoiceHeader)
I use these binding sources to set all the bound properties at design time:
There is one combo which chooses the customer and stores it result in bindingSourceInvoiceHeader.Customer
and another combo which stores the selected contact to bindingSourceInvoiceHeader.Contact
As I mentioned before, I want to place this user-control on another form, and re-bind to a binding source on the main form: to explain.
I'll place this user control, on my invoice editing form. This form will have a binding source for all customers, and a binding source for invoice headers.
I have played with adding a couple of BindingSource properties to my user-control, and when the value changes I run through all the controls on the user form and re-create the bindings - a little messy I feel.
/// <summary><br/>
/// Re-bind controls to a new binding source.<br/>
/// </summary><br/>
/// <param name="sourceControl">Container control</param><br/>
/// <param name="newSource">New binding soruce</param><br/>
public BindingSource RebindControls(Control sourceControl, BindingSource newSource)<br/>
{<br/>
if (newSource == null) return null;<br/>
<br/>
foreach (Control control in sourceControl.Controls)<br/>
{<br/>
ControlBindingsCollection myBindings = control.DataBindings;<br/>
List<Binding> newBindings = new List<Binding>();<br/>
foreach (Binding myBinding in myBindings)<br/>
{<br/>
if ((myBinding.DataSource == myBinding.DataSource))<br/>
{<br/>
Binding newBinding = new Binding(myBinding.PropertyName, newSource, myBinding.BindingMemberInfo.BindingMember,<br/>
myBinding.FormattingEnabled, myBinding.DataSourceUpdateMode, myBinding.NullValue,<br/>
myBinding.FormatString, myBinding.FormatInfo);<br/>
newBindings.Add(newBinding);<br/>
}<br/>
}<br/>
control.DataBindings.Clear();<br/>
foreach (Binding myBinding in newBindings)<br/>
{<br/>
control.DataBindings.Add(myBinding);<br/>
}<br/>
}<br/>
return newSource;<br/>
}<br/>
There are problems with this.
So (Apologies for the over details)
Does anyone know of an elegant way of re-assigning bindings.
I know I can do it through code, by assigning control bindings programmatically, but I feel the doing it all at design time, and re-assigning is neater.
Is there any way of synchronising two binding sources (Should I be looking at currency manager or binding manager)
Again - sorry for the length of this post, and thanks for your time.