Synchronize multiple controls to the same data source (Windows Forms .NET)

During the implementation of data binding in Windows Forms, multiple controls are bound to the same data source. In the following situations, it's necessary to ensure that the bound properties of the control remain synchronized with each other and the data source:

In the former case, you can use a BindingSource to bind the data source to the controls. In the latter case, you use a BindingSource and handle the BindingComplete event and call EndCurrentEdit on the associated BindingManagerBase.

Example of bind controls using BindingSource

The following code example demonstrates how to bind three controls, two textbox controls, and a DataGridView control to the same column in a DataSet using a BindingSource component. The example demonstrates how to handle the BindingComplete event. It ensures that when the text value of one textbox is changed, the other textbox and the DataGridView control are updated with the correct value.

The example uses a BindingSource to bind the data source and the controls. Alternatively, you can bind the controls directly to the data source and retrieve the BindingManagerBase for the binding from the form's BindingContext and then handle the BindingComplete event for the BindingManagerBase. For more information on binding the data source and the controls, see the help page about the BindingComplete event of 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

See also