Oct 9, 2007

Win App: BindingNavigator

The BindingNavigator control represents a standardized way to navigate and manipulate data on a form. In most cases, a BindingNavigator is paired with a BindingSource control to move through data records on a form and interact with them. In these cases, the BindingSource property is set to the associated System.Windows.Forms.BindingSource component that acts as a data source.

By default, the BindingNavigator control's user interface (UI) is composed of a series of ToolStrip buttons, text boxes, and static text elements for most common data-related actions, such as adding data, deleting data, and navigating through data. Each of these controls can be retrieved or set through an associated member of the BindingNavigator control. Likewise, there is also a one-to-one correspondence to members within the BindingSource class that programmatically perform the same functionality, as shown in the following table.

UI Control

BindingNavigator member

BindingSource member

Move First

MoveFirstItem

MoveFirst

Move Previous

MovePreviousItem

MovePrevious

Current Position

PositionItem

Current

Count

CountItem

Count

Move Next

MoveNextItem

MoveNext

Move Last

MoveLastItem

MoveLast

Add New

AddNewItem

AddNew

Delete

DeleteItem

RemoveCurrent

Adding a BindingNavigator control to a form and binding it to a data source, such as a BindingSource, will automatically establish the relationships in this table.

You can use one of the following techniques to customize this toolbar:

  • Create the BindingNavigator with the BindingNavigator(Boolean) constructor, which accepts a Boolean addStandardItems parameter, and set this parameter to false. Then add the desired ToolStripItem objects to the Items collection.

  • If a great deal of customization is desired, or the custom design will be reused, derive a class from BindingNavigator and override the AddStandardItems method to define additional or alternate standard items.

ExampleExample

The following code example demonstrates how to use a BindingNavigator control to move through a data set. The set is contained in a DataView, which is bound to a TextBox control with a BindingSource component.

Visual Basic
 Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Windows.Forms

' This form demonstrates using a BindingNavigator to display
' rows from a database query sequentially.
Public Class Form1
Inherits Form

' This is the BindingNavigator that allows the user
' to navigate through the rows in a DataSet.
Private customersBindingNavigator As New BindingNavigator()

' This is the BindingSource that provides data for
' the Textbox control.
Private customersBindingSource As New BindingSource()

' This is the TextBox control that displays the CompanyName
' field from the the DataSet.
Private companyNameTextBox As New TextBox()

Public Sub New()
' Set up the BindingSource component.
Me.customersBindingNavigator.BindingSource = Me .customersBindingSource
Me.customersBindingNavigator.Dock = DockStyle.Top
Me.Controls.Add(Me.customersBindingNavigator)

' Set up the TextBox control for displaying company names.
Me.companyNameTextBox.Dock = DockStyle.Bottom
Me.Controls.Add(Me.companyNameTextBox)

' Set up the form.
Me.Size = New Size(800, 200)

End Sub

Private Sub Form1_Load( ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load

' Open a connection to the database.
' Replace the value of connectString with a valid
' connection string to a Northwind database accessible
' to your system.
Dim connectString As String = _
"Integrated Security=SSPI;Persist Security Info=False;" + _
"Initial Catalog=Northwind;Data Source=localhost"

Dim connection As New SqlConnection()
connection.ConnectionString = connectString
connection.Open()

' Execute the query.
Dim command As New SqlCommand( _
"Select * From Customers", connection)

Dim reader As SqlDataReader = _
command.ExecuteReader(CommandBehavior.CloseConnection)

' Load the Customers result set into the DataSet.
Dim ds As New DataSet("Northwind Customers")
ds.Load( _
reader, _
LoadOption.OverwriteChanges, _
New String() {"Customers" })

' Assign the DataSet as the DataSource for the BindingSource.
Me.customersBindingSource.DataSource = ds

' Bind the CompanyName field to the TextBox control.
Me.companyNameTextBox.DataBindings.Add( _
New Binding( _
"Text", _
Me.customersBindingSource, _
"Customers.CompanyName" , _
True))
End Sub

<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(new Form1())
End Sub
End Class


--
Regards,
Ethan Liew

No comments:

Post a Comment