BindingManagerBase.BindingComplete 事件


在数据绑定操作完成时发生。Occurs at the completion of a data-binding operation.

 event System::Windows::Forms::BindingCompleteEventHandler ^ BindingComplete;
public event System.Windows.Forms.BindingCompleteEventHandler BindingComplete;
member this.BindingComplete : System.Windows.Forms.BindingCompleteEventHandler 
Public Custom Event BindingComplete As BindingCompleteEventHandler 




下面的代码示例演示如何处理 BindingComplete 事件,并确保在更改其中一个控件值时,绑定到同一数据源的多个控件保持同步。The following code example demonstrates how to handle the BindingComplete event and ensure multiple controls bound to the same data source remain synchronized when one of the control values is changed. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并 InitializeControlsAndData 从窗体的构造函数或 Load 事件处理方法调用方法。To run this example, paste the code into a Windows Form and call the InitializeControlsAndData method from the form's constructor or Load event-handling method.

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 =
    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);

    // Declare the DataSet and add a table and column.
    DataSet set1 = new DataSet();

    // Add some rows to the table.
    set1.Tables[0].Rows.Add("hot chocolate");
    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(); ;
Dim WithEvents bmb As BindingManagerBase

Private Sub InitializeControlsAndData() 
    ' Initialize the controls and set location, size and 
    ' other basic properties.
    Me.dataGridView1 = New DataGridView()
    Me.textBox1 = New TextBox()
    Me.textBox2 = New TextBox()
    Me.dataGridView1.ColumnHeadersHeightSizeMode = _
    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)
    ' Declare the DataSet and add a table and column.
    Dim set1 As New DataSet()
    ' Add some rows to the table.
    set1.Tables(0).Rows.Add("hot chocolate")
    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)

    ' Get the BindingManagerBase for this binding.
    bmb = Me.BindingContext(set1, "Menu")

End Sub

Private Sub bmb_BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs) _
    Handles bmb.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.
    End If
End Sub


BindingComplete 事件在数据绑定操作完成之后发生,这可能包括以下内容:The BindingComplete event occurs after the completion of a data-binding operation, which may include the following:

  • 将信息从数据源推送到其绑定控件的属性。Pushing information from the data source into the property of its bound control.

  • 将信息从绑定控件提取到其关联的数据源中。Pulling information from a bound control into its associated data source.

请注意,无论绑定操作的完成状态如何,都将引发此事件,不过,可以通过检查 BindingCompleteState 与事件关联的参数的属性来确定此状态 BindingCompleteEventArgsNote that this event is raised regardless of the completion status of the binding operation, although this status can be determined by examining the BindingCompleteState property of the BindingCompleteEventArgs parameter associated with the event. BindingCompleteEventArgs 还可能包含错误和异常信息。BindingCompleteEventArgs may also contain error and exception information. 例如,尝试将 null 值从数据源推送到控件的绑定操作 DateTimePicker 将生成异常,因为 DateTime 不能为 nullFor example, a binding operation that attempts to push a null value from a data source into a DateTimePicker control will generate an exception, since a DateTime cannot be null. 在这种情况下,尽管绑定操作没有成功完成,但 BindingComplete 会引发事件,并且事件将包含有关失败的绑定尝试的信息。In this case, although the binding operation does not successfully complete, the BindingComplete event will be raised, and the event will contain information on the failed binding attempt.