将多个控件同步到同一数据源

在 Windows 窗体中实现数据绑定期间,多个控件绑定到同一数据源。 在以下情况下,必须确保控件的绑定属性彼此和数据源保持同步:

在前一种情况下,可以使用 BindingSource 将数据源绑定到控件。 在后一种情况下,使用 BindingSource 并处理 BindingComplete 事件,并在关联的 EndCurrentEdit上调用 BindingManagerBase

使用 BindingSource 绑定控件的示例

下面的代码示例演示如何将三个控件、两个文本框控件和一个DataGridView控件绑定到使用BindingSource组件中的同一DataSet列。 该示例演示如何处理 BindingComplete 事件。 它可确保更改一个文本框的文本值时,其他文本框和 DataGridView 控件将更新为正确的值。

该示例使用 BindingSource 绑定数据源和控件。 或者,可以将控件直接绑定到数据源,并从窗体的 BindingManagerBase 检索绑定的 BindingContext,然后为 BindingComplete 处理 BindingManagerBase 事件。 关于数据源和控件绑定的详细信息,请参阅 BindingComplete 事件的 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

另请参阅