Vorgehensweise: Sicherstellen, dass mehrere Steuerelemente, die an die gleiche Datenquelle gebunden sind, synchronisiert bleiben

Häufig werden beim Arbeiten mit der Datenbindung in Windows Forms mehrere Steuerelemente an dieselbe Datenquelle gebunden. In einigen Fällen ist es möglicherweise erforderlich, zusätzliche Schritte auszuführen, um sicherzustellen, dass die gebundenen Eigenschaften der Steuerelemente miteinander und mit der Datenquelle synchronisiert bleiben. Diese Schritte sind in zwei Situationen erforderlich:

Im ersten Fall können Sie ein BindingSource-Objekt zum Binden der Datenquelle an die Steuerelemente verwenden. Im zweiten Fall verwenden Sie ein BindingSource, verarbeiten das BindingComplete-Ereignis und rufen EndCurrentEdit für das zugeordnete BindingManagerBase-Objekt auf.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie drei Steuerelemente – zwei text-box-Steuerelemente und ein DataGridView-Steuerelement – unter Verwendung einer BindingSource-Komponente an dieselbe Spalte in einem DataSet-Objekt gebunden werden. In diesem Beispiel wird veranschaulicht, wie das BindingComplete-Ereignis verarbeitet und wie sichergestellt wird, dass beim Ändern des Textwerts eines Textfelds das zusätzliche Textfeld und das DataGridView-Steuerelement mit dem richtigen Wert aktualisiert werden.

Im Beispiel wird ein BindingSource-Objekt zum Binden der Datenquelle und der Steuerelemente verwendet. Alternativ können Sie die Steuerelemente direkt an die Datenquelle binden, das BindingManagerBase-Objekt für die Bindung aus dem BindingContext-Objekt des Formulars abrufen und dann das BindingComplete-Ereignis für das BindingManagerBase-Objekt verarbeiten. Ein Beispiel dafür finden Sie auf der Hilfeseite zum BindingComplete-Ereignis von BindingManagerBase.


// Declare the controls to be used.
private BindingSource bindingSource1;
private TextBox textBox1;
private TextBox textBox2;
private DataGridView dataGridView1;

private void InitializeControlsAndDataSource()
{
    // Initialize the controls and set location, size and
    // other basic properties.
    this.dataGridView1 = new DataGridView();
    this.bindingSource1 = new BindingSource();
    this.textBox1 = new TextBox();
    this.textBox2 = new TextBox();
    this.dataGridView1.ColumnHeadersHeightSizeMode =
        DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dataGridView1.Dock = DockStyle.Top;
    this.dataGridView1.Location = new Point(0, 0);
    this.dataGridView1.Size = new Size(292, 150);
    this.textBox1.Location = new Point(132, 156);
    this.textBox1.Size = new Size(100, 20);
    this.textBox2.Location = new Point(12, 156);
    this.textBox2.Size = new Size(100, 20);
    this.ClientSize = new Size(292, 266);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.dataGridView1);

    // Declare the DataSet and add a table and column.
    DataSet set1 = new DataSet();
    set1.Tables.Add("Menu");
    set1.Tables[0].Columns.Add("Beverages");

    // Add some rows to the table.
    set1.Tables[0].Rows.Add("coffee");
    set1.Tables[0].Rows.Add("tea");
    set1.Tables[0].Rows.Add("hot chocolate");
    set1.Tables[0].Rows.Add("milk");
    set1.Tables[0].Rows.Add("orange juice");

    // Set the data source to the DataSet.
    bindingSource1.DataSource = set1;

    //Set the DataMember to the Menu table.
    bindingSource1.DataMember = "Menu";

    // Add the control data bindings.
    dataGridView1.DataSource = bindingSource1;
    textBox1.DataBindings.Add("Text", bindingSource1,
        "Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
    textBox2.DataBindings.Add("Text", bindingSource1,
        "Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
    bindingSource1.BindingComplete +=
        new BindingCompleteEventHandler(bindingSource1_BindingComplete);
}

private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
{
    // Check if the data source has been updated, and that no error has occurred.
    if (e.BindingCompleteContext ==
        BindingCompleteContext.DataSourceUpdate && e.Exception == null)

        // If not, end the current edit.
        e.Binding.BindingManagerBase.EndCurrentEdit();
}

' Declare the controls to be used.
Private WithEvents bindingSource1 As BindingSource
Private WithEvents textBox1 As TextBox
Private WithEvents textBox2 As TextBox
Private WithEvents dataGridView1 As DataGridView


Private Sub InitializeControlsAndDataSource() 
    ' Initialize the controls and set location, size and 
    ' other basic properties.
    Me.dataGridView1 = New DataGridView()
    Me.bindingSource1 = New BindingSource()
    Me.textBox1 = New TextBox()
    Me.textBox2 = New TextBox()
    Me.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
    Me.dataGridView1.Dock = DockStyle.Top
    Me.dataGridView1.Location = New Point(0, 0)
    Me.dataGridView1.Size = New Size(292, 150)
    Me.textBox1.Location = New Point(132, 156)
    Me.textBox1.Size = New Size(100, 20)
    Me.textBox2.Location = New Point(12, 156)
    Me.textBox2.Size = New Size(100, 20)
    Me.ClientSize = New Size(292, 266)
    Me.Controls.Add(Me.textBox2)
    Me.Controls.Add(Me.textBox1)
    Me.Controls.Add(Me.dataGridView1)
    
    ' Declare the DataSet and add a table and column.
    Dim set1 As New DataSet()
    set1.Tables.Add("Menu")
    set1.Tables(0).Columns.Add("Beverages")
    
    ' Add some rows to the table.
    set1.Tables(0).Rows.Add("coffee")
    set1.Tables(0).Rows.Add("tea")
    set1.Tables(0).Rows.Add("hot chocolate")
    set1.Tables(0).Rows.Add("milk")
    set1.Tables(0).Rows.Add("orange juice")
    
    ' Set the data source to the DataSet.
    bindingSource1.DataSource = set1
    
    'Set the DataMember to the Menu table.
    bindingSource1.DataMember = "Menu"
    
    ' Add the control data bindings.
    dataGridView1.DataSource = bindingSource1
    textBox1.DataBindings.Add("Text", bindingSource1, "Beverages", _
        True, DataSourceUpdateMode.OnPropertyChanged)
    textBox2.DataBindings.Add("Text", bindingSource1, "Beverages", _
        True, DataSourceUpdateMode.OnPropertyChanged)


End Sub

Private Sub bindingSource1_BindingComplete(ByVal sender As Object, _
    ByVal e As BindingCompleteEventArgs) Handles bindingSource1.BindingComplete

    ' Check if the data source has been updated, and that no error has occurred.
    If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate _
        AndAlso e.Exception Is Nothing Then

        ' If not, end the current edit.
        e.Binding.BindingManagerBase.EndCurrentEdit()
    End If

End Sub

Kompilieren des Codes

  • Für dieses Codebeispiel benötigen Sie Folgendes:

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

  • Ein Formular mit dem behandelten Load-Ereignis und einem Aufruf der InitializeControlsAndDataSource-Methode im Beispiel aus dem Ereignishandler des Formulars Load.

Weitere Informationen