다음을 통해 공유


How to: Move Through a DataSet with the Windows Forms BindingNavigator Control

As you build data-driven applications, you will often need to display collections of data to users. The BindingNavigator control, in conjunction with the BindingSource component, provides a convenient and extensible solution for moving through a collection and displaying items sequentially.

Example

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

NoteNote

Storing sensitive information, such as a password, within the connection string can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see Securing Connection Strings.

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

// This form demonstrates using a BindingNavigator to display 
// rows from a database query sequentially.
public class Form1 : Form
{
    // This is the BindingNavigator that allows the user
    // to navigate through the rows in a DataSet.
    BindingNavigator customersBindingNavigator = new BindingNavigator();

    // This is the BindingSource that provides data for
    // the Textbox control.
    BindingSource customersBindingSource = new BindingSource();

    // This is the TextBox control that displays the CompanyName
    // field from the the DataSet.
    TextBox companyNameTextBox = new TextBox();

    public Form1()
    {
        // Set up the BindingSource component.
        this.customersBindingNavigator.BindingSource = this.customersBindingSource;
        this.customersBindingNavigator.Dock = DockStyle.Top;
        this.Controls.Add(this.customersBindingNavigator);

        // Set up the TextBox control for displaying company names.
        this.companyNameTextBox.Dock = DockStyle.Bottom;
        this.Controls.Add(this.companyNameTextBox);

        // Set up the form.
        this.Size = new Size(800, 200);
        this.Load += new EventHandler(Form1_Load);
    }

    void Form1_Load(object sender, EventArgs e)
    {   
        // Open a connection to the database.
        // Replace the value of connectString with a valid 
        // connection string to a Northwind database accessible 
        // to your system.
        string connectString = 
            "Integrated Security=SSPI;Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = connectString;
        connection.Open();

        // Execute the query.
        SqlCommand command = new SqlCommand(
            "Select * From Customers", connection);
        SqlDataReader reader = command.ExecuteReader(
            CommandBehavior.CloseConnection);

        // Load the Customers result set into the DataSet.
        DataSet ds = new DataSet("Northwind Customers");
        ds.Load(
            reader, 
            LoadOption.OverwriteChanges, 
            new string[] { "Customers" });

        // Assign the DataSet as the DataSource for the BindingSource.
        this.customersBindingSource.DataSource = ds;

        // Bind the CompanyName field to the TextBox control.
        this.companyNameTextBox.DataBindings.Add(
            new Binding("Text", 
            this.customersBindingSource, 
            "Customers.CompanyName", 
            true));
    }

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}
#using <System.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.EnterpriseServices.dll>
#using <System.Transactions.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Data::SqlClient;
using namespace System::Windows::Forms;

// This form demonstrates using a BindingNavigator to display
// rows from a database query sequentially.
public ref class Form1 : public Form
{
    // This is the BindingNavigator that allows the user
    // to navigate through the rows in a DataSet.
    BindingNavigator^ customersBindingNavigator;

    // This is the BindingSource that provides data for
    // the Textbox control.
    BindingSource^ customersBindingSource;

    // This is the TextBox control that displays the CompanyName
    // field from the the DataSet.
    TextBox^ companyNameTextBox;

public:
    Form1()
    {
        // Set up the BindingSource component.
        this->customersBindingSource = gcnew BindingSource();
        this->companyNameTextBox = gcnew TextBox();
        this->customersBindingNavigator = gcnew BindingNavigator();
        this->customersBindingNavigator->BindingSource =
            this->customersBindingSource;
        this->customersBindingNavigator->Dock = DockStyle::Top;
        this->Controls->Add(this->customersBindingNavigator);

        // Set up the TextBox control for displaying company names.
        this->companyNameTextBox->Dock = DockStyle::Bottom;
        this->Controls->Add(this->companyNameTextBox);

        // Set up the form.
        this->Size = System::Drawing::Size(800, 200);
        this->Load += gcnew EventHandler(this, &Form1::Form1_Load);
    }

private:
    void Form1_Load(Object^ sender, EventArgs^ e)
    {
        // Open a connection to the database.
        // Replace the value of connectString with a valid
        // connection string to a Northwind database accessible
        // to your system.
        String^ connectString =
            "Integrated Security=SSPI;Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";
        SqlConnection^ connection = gcnew SqlConnection();
        connection->ConnectionString = connectString;
        connection->Open();

        // Execute the query.
        SqlCommand^ command = gcnew SqlCommand(
            "Select * From Customers", connection);
        SqlDataReader^ reader = command->ExecuteReader(
            CommandBehavior::CloseConnection);

        // Load the Customers result set into the DataSet.
        DataSet^ ds = gcnew DataSet("Northwind Customers");
        ds->Load(reader, LoadOption::OverwriteChanges,
            gcnew array<String^> {"Customers"});

        // Assign the DataSet as the DataSource for the
        // BindingSource.
        this->customersBindingSource->DataSource = ds->Tables[0];

        // Bind the CompanyName field to the TextBox control.
        this->companyNameTextBox->DataBindings->Add(gcnew Binding("Text",
            this->customersBindingSource, "Customers.CompanyName", true));
    }
};
import System.*;
import System.Collections.Generic.*;
import System.ComponentModel.*;
import System.Data.*;
import System.Drawing.*;
import System.Data.SqlClient.*;
import System.Windows.Forms.*;

// This form demonstrates using a BindingNavigator to display 
// rows from a database query sequentially.
class Form1 extends Form
{
    // This is the BindingNavigator that allows the user
    // to navigate through the rows in a DataSet.
    private BindingNavigator customersBindingNavigator = new BindingNavigator();

    // This is the BindingSource that provides data for
    // the Textbox control.
    private BindingSource customersBindingSource = new BindingSource();

    // This is the TextBox control that displays the CompanyName
    // field from the the DataSet.
    private TextBox companyNameTextBox = new TextBox();

    public Form1()
    {
        // Set up the BindingSource component.
        this.customersBindingNavigator.set_BindingSource(this.
            customersBindingSource);
        this.customersBindingNavigator.set_Dock(DockStyle.Top);
        this.get_Controls().Add(this.customersBindingNavigator);
        // Set up the TextBox control for displaying company names.
        this.companyNameTextBox.set_Dock(DockStyle.Bottom);
        this.get_Controls().Add(this.companyNameTextBox);
        // Set up the form.
        this.set_Size(new Size(800, 200));
        this.add_Load(new EventHandler(Form1_Load));
    } //Form1

    private void Form1_Load(Object sender, EventArgs e)
    {
        // Open a connection to the database.
        // Replace the value of connectString with a valid 
        // connection string to a Northwind database accessible 
        // to your system.
        String connectString = "Integrated Security=SSPI;Persist"
            + " Security Info=False;" + "Initial Catalog=Northwind;"
            + " Data Source=localhost";
        SqlConnection connection = new SqlConnection();
        connection.set_ConnectionString(connectString);
        connection.Open();
        // Execute the query.
        SqlCommand command = new SqlCommand("Select * From Customers", 
            connection);
        SqlDataReader reader = command.ExecuteReader(CommandBehavior.
            CloseConnection);
        // Load the Customers result set into the DataSet.
        DataSet ds = new DataSet("Northwind Customers");
        ds.Load(reader, LoadOption.OverwriteChanges, new String[] { "Customers" });
        // Assign the DataSet as the DataSource for the BindingSource.
        this.customersBindingSource.set_DataSource(ds);
        // Bind the CompanyName field to the TextBox control.
        this.companyNameTextBox.get_DataBindings().Add(new Binding("Text",
            this.customersBindingSource, "Customers.CompanyName", true));
    } //Form1_Load
} //Form1

Compiling the Code

This example requires:

  • References to the System, System.Data, System.Drawing, System.Windows.Forms and System.Xml assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-Line Building. You can also build this example in Visual Studio by pasting the code into a new project.

See Also

Tasks

How to: Bind a Windows Forms Control to a Type

Reference

BindingSource
DataGridView
BindingSource

Other Resources

BindingNavigator Control (Windows Forms)
BindingSource Component