[sourcecode language="vb"]
Function RetrieveCustomerContacts() As DataSet Dim ds As DataSet Try ds = RunSQLWithDataSet("Select CustomerID, " & _ "CompanyName, ContactName, NoOfCustomerVisits " & _ "from customers", ConnectionString, "Customers") Catch ex As Exception ds = Nothing End Try Return ds End Function
[/sourcecode]
Next, consider a typed DataSet named Customers with the following fields that I’ve added to the project as dsCustomers.xsd (part of the XSD has been omitted to conserve space):
<xs:sequence> <xs:element name="CustomerID" type="xs:string" /> <xs:element name="CompanyName" type="xs:string" /> <xs:element name="ContactName" type="xs:string" minOccurs="0" /> <xs:element name="NoOfCustomerVisits" type="xs:int" minOccurs="0" /> </xs:sequence>
You can complete the process by adding the following code in the form’s Load event:
Dim ds As DataSet Dim oClass As New SomeComponent.Class1 Try ds = oClass.RetrieveCustomerContacts dsCustomers.Merge(ds) Catch ex As Exception sbarmain.Text = ex.Message End Try
This code calls the RetrieveCustomerContacts class to retrieve the DataSet and merge it into the typed DataSet for your form. Of course, you can also perform this type of data binding with other controls such as the listbox and combobox controls. There is another cool feature that comes into play. The same code I just showed can be used to cause the typed DataSet to be loaded into your combobox. Then you can extract the chosen value with just a single line of code in the SelectedIndexChanged event:
txtSelectedID.Text = ComboBox1.SelectedValue
Of course, if you are binding the txtSelectedID control to a class or structure where the property is called CustomerID, you could do this instead of putting the data directly into the control:
CustomerContacts.CustomerID = ComboBox1.SelectedValue
This would update the class’s CustomerID field and the data binding would update the value shown in the bound textbox.

Private WithEvents thisCurrencyManager As CurrencyManager 'Variable defs go here 'Form Load builds the dataset, merges it with CustomerInfo1, 'then binds the controls by calling BindControls Private Sub BindControls(ByVal thisDataTable As DataTable) ' Bind a TextBox control to a DataTable column 'in a DataSet. txtCompany.DataBindings.Add("Text", thisDataTable, "CompanyName") txtName.DataBindings.Add("Text", thisDataTable, "ContactName") txtCity.DataBindings.Add("Text", thisDataTable, "City") txtCustomerID.DataBindings.Add("Text", thisDataTable, "CustomerID") ' Specify the CurrencyManager for the DataTable. thisCurrencyManager = _ CType(Me.BindingContext(thisDataTable), CurrencyManager) ' Set the initial Position of the control. thisCurrencyManager.Position = 0 End Sub Private Sub MoveNext(ByVal thisCurrencyManager As CurrencyManager) If thisCurrencyManager.Position = thisCurrencyManager.Count - 1 Then MessageBox.Show("You're at end of the records") Else thisCurrencyManager.Position += 1 CheckChanges() End If End Sub Private Sub MoveFirst(ByVal thisCurrencyManager As CurrencyManager) thisCurrencyManager.Position = 0 CheckChanges() End Sub Private Sub MovePrevious(ByVal thisCurrencyManager As CurrencyManager) If thisCurrencyManager.Position = 0 Then MessageBox.Show( _ "You're at the beginning of the records.") Else thisCurrencyManager.Position -= 1 CheckChanges() End If End Sub Private Sub MoveLast(ByVal thisCurrencyManager As CurrencyManager) thisCurrencyManager.Position = thisCurrencyManager.Count - 1 CheckChanges() End Sub 'Button click events go here Private Sub txtName_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles txtName.TextChanged 'Exit if starting up If IsNothing(thisCurrencyManager) Then Exit Sub End If StartEditMode() End Sub Private Sub txtCompany_TextChanged(ByVal sender As _ System.Object,ByVal e As System.EventArgs) _ Handles txtCompany.TextChanged StartEditMode() End Sub Private Sub txtCity_TextChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles txtCity.TextChanged StartEditMode() End Sub Sub StartEditMode() cmdSaveChanges.Enabled = True End Sub Sub EndEditMode() Me.BindingContext(CustomerInfo1.Customers).EndCurrentEdit() End Sub Private Sub thisCurrencyManager_PositionChanged( _ ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles thisCurrencyManager.PositionChanged cmdSaveChanges.Enabled = False End Sub Private Sub cmdNew_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdNew.Click thisCurrencyManager.AddNew() End Sub 'Other subs go here
The first line of code in Figure 3 defines the CurrencyManager for my application. It’s defined with WithEvents to allow the use of its events if I need them:
Private WithEvents thisCurrencyManager As CurrencyManager
The BindControls method is where you see the CurrencyManager in action. The first few lines bind four fields from the DataSet to the Text property of the controls:
txtCompany.DataBindings.Add("Text", thisDataTable, "CompanyName") txtName.DataBindings.Add("Text", thisDataTable, "ContactName") txtCity.DataBindings.Add("Text", thisDataTable, "City") txtCustomerID.DataBindings.Add("Text", thisDataTable, "CustomerID")
The last two lines of BindControls actually use the CurrencyManager. The first of these lines sets the CurrencyManager to the BindingContext of the data source (a DataTable in this case):
thisCurrencyManager = CType(Me.BindingContext(thisDataTable), _ CurrencyManager)
The next line sets the initial position to 0 (the first record):
thisCurrencyManager.Position = 0
Now, I can proceed to the CurrencyManager. The Move methods all change the position within the DataSet by using the CurrencyManager. For instance, the MoveNext method moves the current element pointer forward in the data source as long as the current position is not the last element:
If thisCurrencyManager.Position = _ thisCurrencyManager.Count - 1 Then MessageBox.Show("You're at end of the records") Else thisCurrencyManager.Position += 1 CheckChanges() End If
The CheckChanges method is called each time the position changes to determine if the DataSet has any changes.
I was using a DataTable as the data source. The EndCurrentEdit method was being fired by the EndEditMode procedure with the following line of code:
Me.BindingContext(CustomerInfo1, "Customers").EndCurrentEdit()
This did not work; the DataSet showed no changes even though the data did, in fact, change. Finally, I realized I was using the wrong context. The following context worked just fine:
Me.BindingContext(CustomerInfo1.Customers).EndCurrentEdit()
In this case, I am passing only the DataTable which is indeed the data source for the controls. The funny thing is, the original code didn’t generate any errors when it executed; it just didn’t work. I’m sure the line did actually execute because when I changed the table name (Customers) to CustomersX, the line generated a runtime error. As I said, the code was executing, it was just pointing to the wrong data source.
[Share with Me] Data Binding in Visual Basic.Net
No comments:
Post a Comment