JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Navigating in Datasets

You usually add navigation controls, such as the buttons at the bottom of the DataBinding example in Figure 21.3, to let the user move from record to record. When the user clicks the > button, for example, the bound data from the next field is displayed in the text box, so the name displayed in the text box changes from the first author's last name, White, to the last name of the next author, Green. The >> button moves to the last record, as you'd expect; the << button moves to the first record, and so on. And note that the code also displays the user's current location in a label control at the bottom.

To set the current record bound to the various controls in a form, you use the form's BindingContext property (see "Using the BindingContext Class" in this chapter), which is inherited from the Control class. The binding context sets the location in various data sources that are bound in the form. To see how to use the binding context, I'll start by seeing how the code displays the current location in the authors table.

Tip 

Note that, unlike in ADO's record sets, ADO.NET's datasets do not use the concept of a current record. To work with the record currently displayed in simple data-bound controls, you use a form's BindingContext property instead.


Main Page

Displaying the Current Location

When the DataBinding example first loads, it fills its dataset from its data adapter, as we've done in the previous chapter. It also displays the current position in the dataset's binding context for the authors table by using the form's Binding Context property's Position and Count members, like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    DataSet11.Clear()
    OleDbDataAdapter1.Fill(DataSet11)
    Label1.Text = (Me.BindingContext(DataSet11, "authors").Position + _
        1).ToString & " of " & Me.BindingContext(DataSet11, _
        "authors").Count.ToString
End Sub

This code gives you the "3 of 23" text you see in the bottom of Figure 21.3 as the user moves through the dataset.


Main Page

Moving to the Next Record

So how does the user actually move through the dataset? They use the arrow buttons in the DataBinding example for navigation; when the user clicks the > arrow, I just increment the Position property of the binding context for the authors table, and then display the new location in the label between the navigation buttons:

Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
    Me.BindingContext(DataSet11, "authors").Position = _
        (Me.BindingContext(DataSet11, "authors").Position + 1)
    Label1.Text = (((Me.BindingContext(DataSet11, "authors").Position _
        + 1).ToString & " of ") & Me.BindingContext(DataSet11, _
        "authors").Count.ToString)
End Sub

Note that if you try to move beyond the end of the record set, the Position property isn't incremented.


Main Page

Moving to the Previous Record

In the same way, when the user clicks the < button to move to the previous record, the code simply decrements the Position value:

Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Me.BindingContext(DataSet11, "authors").Position = _
        (Me.BindingContext(DataSet11, "authors").Position - 1)
    Label1.Text = (((Me.BindingContext(DataSet11, "authors").Position _
        + 1).ToString & " of ") & Me.BindingContext(DataSet11, _
        "authors").Count.ToString)
End Sub

Main Page

Moving to the First Record

Moving to the first record in the binding context for the authors table is easy; you just set the Position property to 0:

Private Sub Button4_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button4.Click
    Me.BindingContext(DataSet11, "authors").Position = 0
    Label1.Text = (((Me.BindingContext(DataSet11, "authors").Position _
        + 1).ToString & " of ") & Me.BindingContext(DataSet11, _
        "authors").Count.ToString)
End Sub

Main Page

Moving to the Last Record

Moving to the last record is also easy, because we know that the Count property returns the total number of records in the table:

Private Sub Button5_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button5.Click
    Me.BindingContext(DataSet11, "authors").Position = _
        Me.BindingContext(DataSet11, "authors").Count - 1
    Label1.Text = (((Me.BindingContext(DataSet11, "authors").Position _
        + 1).ToString & " of ") & Me.BindingContext(DataSet11, _
        "authors").Count.ToString)
End Sub

And that gives us a good introduction to using navigation controls to move around in a dataset simply bound to controls. You also might note that even the controls that are complex-bound display the current binding context record; as you see in Figure 21.3, the current record is highlighted in the checked list box and in the list box. Also, the data grid at the upper right of the figure displays a small arrow indicating the current record.

The DataBinding example illustrates one more point that is good to know as well—the difference between the DisplayMember and ValueMember properties, and I'll take a look at that now.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor