Switching between datasources using bound controls

B

bparr

All,

I am using Windows Forms and have multiple XML datasets feeding various
controls. Here is the situation.

I have a master dataset that saves a document to the drive. Out of 50
fields in the XML document, there are 5 that have defaults loaded from
a different XML dataset when the new form is created.

In pseudocode

If New
Fee Dataset = openxml Fees.xml
txtFee1.text = Fee Dataset (Fee1)
Else
Document Dataset = openxml Document.xml
txtFee1.text = Document dataset (Fee1)
End If

The data is bound properly from the master dataset when I open an
document but won't load default data from the fee.xml file when a new
document is created.

I am assuming if I am creating a new document, I need to drop the
databinding for those fields from the document, and assign it to the
fee xml file, then switch it back. This is apparently wrong or I am
doing it wrong.

Any and all help is greatly appreciated

Thanks,
jaxmeier
 
E

ECathell

This is the way I do it....Some of its specific to my needs but you should
get the idea....


Private Sub bindTextBoxes()
clearbindings()
Me.mCustomerCM = BindingContext(mCustomerCollection)
Me.txtCustNo.DataBindings.Add(New Binding("text", mCustomerCollection,
"Custno"))
Me.txtCustAbbreviation.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustAbbr"))
Me.txtCustShortName.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustShortName"))
Me.txtCustName.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustName"))
Me.txtCustAddress.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustAddress"))
Me.txtCustCity.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustCity"))
Me.txtCustState.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustState"))
Me.txtCustZip.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustZip"))
Me.txtCustMfgNo.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustMfgNo"))
Me.txtCustDistPack.DataBindings.Add(New Binding("text",
mCustomerCollection, "CustDistPack"))
Me.txtCustNo.ReadOnly = True

End Sub
Private Sub clearbindings()
Try
Me.txtCustNo.ReadOnly = False

Dim c As New Control

For Each c In Me.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
Console.WriteLine(c.Name)
c.DataBindings.Clear()
c.Text = Nothing
End If

Next
Catch ex As InvalidCastException
'Catches a spurious error
Debug.WriteLine(ex.Message)

Catch ex As Exception

End Try
End Sub
 

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