BindingManagerBase.BindingComplete 事件

定义

在数据绑定操作完成时发生。

public event System.Windows.Forms.BindingCompleteEventHandler BindingComplete;

事件类型

示例

下面的代码示例演示如何处理 BindingComplete 事件,并确保当其中一个控件值发生更改时,绑定到同一数据源的多个控件保持同步。 若要运行此示例,请将代码粘贴到 Windows 窗体中, InitializeControlsAndData 然后从窗体的构造函数或 Load 事件处理方法调用 方法。

private void InitializeControlsAndData()
{
    // Initialize the controls and set location, size and 
    // other basic properties.
    this.dataGridView1 = new DataGridView();
    
    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");

    // Add the control data bindings.
    dataGridView1.DataSource = set1;
    dataGridView1.DataMember = "Menu";
    textBox1.DataBindings.Add("Text", set1,
        "Menu.Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
    textBox2.DataBindings.Add("Text", set1,
        "Menu.Beverages", true, DataSourceUpdateMode.OnPropertyChanged);

    BindingManagerBase bmb = this.BindingContext[set1, "Menu"];
    bmb.BindingComplete += new BindingCompleteEventHandler(bmb_BindingComplete);
}

private void bmb_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(); ;
}

注解

BindingComplete 事件在数据绑定操作完成后发生,其中可能包括以下内容:

  • 将数据源中的信息推送到其绑定控件的 属性中。

  • 将绑定控件中的信息拉取到其关联的数据源中。

请注意,无论绑定操作的完成状态如何,都会引发此事件,尽管可以通过检查 BindingCompleteState 与事件关联的参数的 BindingCompleteEventArgs 属性来确定此状态。 BindingCompleteEventArgs 还可能包含错误和异常信息。 例如,尝试将 null 值从数据源推送到控件的 DateTimePicker 绑定操作将生成异常,因为 DateTime 不能是 null。 在这种情况下,尽管绑定操作未成功完成,但 BindingComplete 将引发事件,并且事件将包含有关绑定尝试失败的信息。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅