Understanding Databinding

In Visual Basic 6, the RecordSet object gives you the ability to bind data from a database to text boxes, data grids, and labels. The RecordSet keeps track of the positioning through its event model. You simply call methods such as MoveFirst, MoveNext, MovePrevious, and MoveLast to navigate records in the RecordSet, and the RecordSet magically handles it for you.

In .NET, the DataSet binds its data to controls. Because the DataSet is simply a container of data, it has no knowledge of positioning. To implement code like MoveFirst and MoveNext, you must set up a BindingContext for the controls that are being databound. .NET uses the inherited CurrencyManager class to internally handle the navigation within the BindingContext of the DataSet and the controls that it's binding to.

To understand this better, consider the following Visual Basic .NET code for the btnNavNext that the Data Form Wizard created:

Private Sub btnNavNext_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnNavNext.Click

  Me.BindingContext(objdsCustomers, "Customers").Position = _
      (Me.BindingContext(objdsCustomers, "Customers").Position + 1)
  Me.objdsCustomers_PositionChanged()

End Sub

To navigate, the current position must be maintained within the DataSet. You can keep track of this yourself, but it's easier to let the Position property of the BindingContext class handle the positioning for you. When controls are databound on a Windows Form, they're part of the BindingContext of the form, so using the Position property is an easy way to handle navigation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.201