Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleNavigate a database with bound controls in VB .NET
Keywordsnavigate, database, bound control, VB.NET
CategoriesDatabase, VB.NET
 
Bind controls to a database usual (see HowTo: Bind simple controls to a database in VB .NET for instructions).

The position of a Table in a DataSet is controlled by a binding context. You can use the form's BindingContext method to get the binding context associated with a DataSet and table. This object's Position property determines its position within the table.

To move to the first record, set Position = 0. To move to the previous or next record, subtract or add 1 to Position. To move to the last record, use the binding context's Count property get the number of records and set Position equal the number of records minus 1.

To add a record, call the binding context's AddNew method. To remove a record, make the user connfirm the deletion and then calls the binding context's RemoveAt method, passing it the index of the record to remove.

 
' Move to the previous record in the dsUsers 
' DataSet's People table.
Private Sub btnPrev_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnPrev.Click
    Me.BindingContext(dsUsers, "People").Position -= 1
End Sub

' Move to the next record in the dsUsers 
' DataSet's People table.
Private Sub btnNext_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnNext.Click
    Me.BindingContext(dsUsers, "People").Position += 1
End Sub

' Move to the last record in the dsUsers 
' DataSet's People table.
Private Sub btnLast_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnLast.Click
    Me.BindingContext(dsUsers, "People").Position = _
        Me.BindingContext(dsUsers, "People").Count - 1
End Sub

' Add a new record.
Private Sub btnAdd_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAdd.Click
    Me.BindingContext(dsUsers, "People").AddNew()
End Sub

' Delete the current record.
Private Sub btnDelete_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnDelete.Click
    If MsgBox("Are you sure you want to delete the record " & _
        "for " & _
        txtFirstName.Text & " " & txtLastName.Text & "?", _
        MsgBoxStyle.Question Or MsgBoxStyle.YesNo, _
        "Confirm") = MsgBoxResult.Yes _
    Then
        Me.BindingContext(dsUsers, "People"). _
            RemoveAt(Me.BindingContext(dsUsers, _
                "People").Position)
    End If
End Sub
 
For more information on using databases with VB .NET, see my book Visual Basic .NET Database Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated