Synkronisera flera kontroller med samma datakälla

Under implementeringen av databindning i Windows Forms är flera kontroller bundna till samma datakälla. I följande situationer är det nödvändigt att se till att kontrollens bundna egenskaper förblir synkroniserade med varandra och datakällan:

I det tidigare fallet kan du använda en BindingSource för att binda datakällan till kontrollerna. I det senare fallet använder du en BindingSource och hanterar händelsen BindingComplete och anropar EndCurrentEdit på den associerade BindingManagerBase.

Exempel på bindningskontroller med BindingSource

Följande kodexempel visar hur du binder tre kontroller, två textrutekontroller och en DataGridView kontroll till samma kolumn i en DataSet med hjälp av en BindingSource komponent. Exemplet visar hur du hanterar BindingComplete händelsen. Det säkerställer att när textvärdet för en textruta ändras uppdateras den andra textrutan och kontrollen DataGridView med rätt värde.

I exemplet används en BindingSource för att binda datakällan och kontrollerna. Du kan också binda kontrollerna direkt till datakällan och hämta BindingManagerBase för bindningen från formulärets BindingContext och sedan hantera händelsen BindingComplete för BindingManagerBase. Mer information om hur du binder datakällan och kontrollerna finns på hjälpsidan om BindingComplete händelse för BindingManagerBase.

public Form1() 
{
    InitializeComponent();
    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);
}

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();
}
Public Class Form1
    Private Sub InitializeControlsAndDataSource()
        ' Add a table and column to 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
        
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitializeControlsAndDataSource()
    End Sub
End Class

Se även