Gewusst wie: Navigieren durch ein DataSet mithilfe des BindingNavigator-Steuerelements in Windows Forms

Wenn Sie datengesteuerte Anwendungen erstellen, müssen Sie den Benutzern häufig Datenauflistungen anzeigen. Das BindingNavigator-Steuerelement bietet zusammen mit der BindingSource-Komponente eine bequeme und erweiterbare Lösung, um durch eine Auflistung zu navigieren und Elemente der Reihe nach anzuzeigen.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mit einem BindingNavigator-Steuerelement Daten durchlaufen werden. Das Dataset ist in einer DataView enthalten, die an ein TextBox-Steuerelement mit einer BindingSource-Komponente gebunden ist.

Tipp

Das Speichern vertraulicher Informationen wie Kennwörter innerhalb der Verbindungszeichenfolge kann die Sicherheit einer Anwendung beeinträchtigen. Die Windows-Authentifizierung bzw. integrierte Sicherheit stellt eine Möglichkeit der Steuerung des Datenbankzugriffs dar, die eine höhere Sicherheit gewährleistet. Weitere Informationen finden Sie unter Schützen von Verbindungsinformationen (ADO.NET).

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(True)

    ' 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)
        AddHandler Me.Load, AddressOf Form1_Load

    End Sub 'New


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' 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(connectString)
        Try

            Dim dataAdapter1 As New SqlDataAdapter( _
                New SqlCommand("Select * From Customers", connection))
            Dim ds As New DataSet("Northwind Customers")
            ds.Tables.Add("Customers")
            dataAdapter1.Fill(ds.Tables("Customers"))

            ' Assign the DataSet as the DataSource for the BindingSource.
            Me.customersBindingSource.DataSource = ds.Tables("Customers")

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

    End Sub 'Form1_Load


    <STAThread()> _
    Public 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(true);

    // 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";

        using (SqlConnection connection = new SqlConnection(connectString))
        {

            SqlDataAdapter dataAdapter1 = 
                new SqlDataAdapter(new SqlCommand("Select * From Customers",connection));
            DataSet ds = new DataSet("Northwind Customers");
            ds.Tables.Add("Customers");
            dataAdapter1.Fill(ds.Tables["Customers"]);

            // Assign the DataSet as the DataSource for the BindingSource.
            this.customersBindingSource.DataSource = ds.Tables["Customers"];

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

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

Kompilieren des Codes

Für dieses Beispiel sind erforderlich:

  • Verweise auf die Assemblys System, System.Data, System.Drawing, System.Windows.Forms und System.Xml.

Informationen zum Erstellen dieses Beispiels über die Befehlszeile für Visual Basic oder Visual C# finden Sie unter Erstellen von der Befehlszeile aus (Visual Basic) oder Erstellen über die Befehlszeile mit csc.exe. Sie können dieses Beispiel auch in Visual Studio erstellen, indem Sie den Code in ein neues Projekt einfügen. Weitere Informationen finden Sie unter Gewusst wie: Kompilieren und Ausführen eines vollständigen Windows Forms-Codebeispiels mit Visual Studio und Gewusst wie: Kompilieren und Ausführen eines vollständigen Windows Forms-Codebeispiels mit Visual Studio und Gewusst wie: Kompilieren und Ausführen eines vollständigen Windows Forms-Codebeispiels mit Visual Studio und Gewusst wie: Kompilieren und Ausführen eines vollständigen Windows Forms-Codebeispiels mit Visual Studio und Gewusst wie: Kompilieren und Ausführen eines vollständigen Windows Forms-Codebeispiels mit Visual Studio.

Siehe auch

Aufgaben

Gewusst wie: Binden eines Windows Forms-Steuerelements an einen Typ

Referenz

BindingSource

DataGridView

BindingSource

Weitere Ressourcen

BindingNavigator-Steuerelement (Windows Forms)

BindingSource-Komponente