Some controls such as the DataGrid control provide their own forms of navigation. If you bind a DataGrid to a DataSet, it allows the user to examine the DataSet object’s tables, view and edit data, and follow links between the tables. The DataGrid provides its own methods for navigating through the data. For simpler controls, such as the TextBox, which can display only one data value at a time, you must provide some means for the program to navigate through the data source’s records.
A data source manages its position within its data by using a CurrencyManager object. The CurrencyManager supervises the list of Binding objects that bind the data source to controls such as TextBoxes.
The following table describes the CurrencyManager object’s most useful properties.
PROPERTY | PURPOSE |
Bindings | A collection of the bindings that the object manages. |
Count | Returns the number of rows associated with the CurrencyManager. |
Current | Returns a reference to the current data object (row). |
List | Returns an object that implements the IList interface that provides the data for the CurrencyManager. For example, if the data source is a DataSet or DataTable, this object is a DataView. |
Position | Gets or sets the current position within the data. For example, in a DataTable this is the row number. |
The CurrencyManager also provides some methods for manipulating the data. The following table describes the CurrencyManager object’s most useful methods.
METHOD | PURPOSE |
AddNew | Adds a new item to the data source |
CancelCurrentEdit | Cancels the current editing operation |
EndCurrentEdit | Ends the current editing operation, accepting any changes |
Refresh | Refills the bound controls |
RemoveAt | Removes the data source item at a specified index |
The CurrencyManager class raises a PositionChanged event when its position in the data changes.
Example program BindSimple, which is available for download on the book’s website, uses the following code to navigate through a DataSet:
Public Class Form1
Private WithEvents MyCurrencyManager As CurrencyManager
Private Sub Form1_Load() Handles MyBase.Load
Me.BooksTableAdapter.Fill(Me.BooksDataSet.Books)
' Get the CurrencyManager.
MyCurrencyManager = DirectCast(Me.BindingContext(
BooksBindingSource), CurrencyManager)
' Display the record number.
MyCurrencyManager_PositionChanged()
End Sub
' Move to the previous record.
Private Sub btnPrev_Click() Handles btnPrev.Click
If MyCurrencyManager.Position = 0 Then
Beep()
Else
MyCurrencyManager.Position -= 1
End If
End Sub
' Move to the next record.
Private Sub btnNext_Click() Handles btnNext.Click
If MyCurrencyManager.Position >= MyCurrencyManager.Count - 1 Then
Beep()
Else
MyCurrencyManager.Position += 1
End If
End Sub
' Go to the first record.
Private Sub btnFirst_Click() Handles btnFirst.Click
MyCurrencyManager.Position = 0
End Sub
' Go to the last record.
Private Sub btnLast_Click() Handles btnLast.Click
MyCurrencyManager.Position = MyCurrencyManager.Count - 1
End Sub
Private Sub MyCurrencyManager_PositionChanged() _
Handles MyCurrencyManager.PositionChanged
lblPosition.Text =
(MyCurrencyManager.Position + 1) & " of " & MyCurrencyManager.Count
End Sub
' Add a record.
Private Sub btnAdd_Click() Handles btnAdd.Click
MyCurrencyManager.AddNew()
txtTitle.Focus()
End Sub
' Delete the current record.
Private Sub btnDelete_Click() Handles btnDelete.Click
If MessageBox.Show("Are you sure you want to remove this record?",
"Confirm?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) =
Windows.Forms.DialogResult.Yes _
Then
MyCurrencyManager.RemoveAt(MyCurrencyManager.Position)
End If
End Sub
End Class
When the form loads, the program fills its data set and saves a reference to a CurrencyManager object that controls the data set’s Books table. It then calls subroutine MyCurrency Manager_PositionChanged to display the current record’s index (this is described shortly).
The first, last, previous, and next record buttons all work by changing the CurrencyManager’s Position property. For example, the previous record button’s event handler checks whether the current position is greater than zero, and if it is the code decreases the position by one.
Similarly, the next record button increases the current position by one if the CurrencyManager is not already displaying the last record.
The first and last record buttons set the position to the indexes of the first and last records, respectively.
Whenever the CurrencyManager’s position changes, its PositionChanged event handler executes. This code displays the current record’s index in a label for the user to see.
When the user clicks the add record button, the code calls the CurrencyManager’s AddNew method to make a new record. It also sets focus to the first text box to make filling in the new record easier.
Finally, when the user clicks the delete record button, the code confirms the deletion and then calls the CurrencyManager’s RemoveAt method to delete the record.
Figure 19-16 shows the BindSimple program in action.
Example program BindSimpleMemoryDataSet, which is available for download on the book’s website, is similar to program BindSimple except it uses a DataSet built-in memory rather than one loaded from a database.