BindingSource.AddingNew 事件

定义

在将项添加到基础列表之前发生。Occurs before an item is added to the underlying list.

public:
 event System::ComponentModel::AddingNewEventHandler ^ AddingNew;
public event System.ComponentModel.AddingNewEventHandler AddingNew;
member this.AddingNew : System.ComponentModel.AddingNewEventHandler 
Public Custom Event AddingNew As AddingNewEventHandler 

异常

NewObject 与该列表中包含的类型不属于同一类型。NewObject is not the same type as the type contained in the list.

示例

下面的代码示例使用 BindingSource 组件将列表绑定到 DataGridView 控件。The following code example uses a BindingSource component to bind a list to a DataGridView control. AddingNew 事件处理程序将新项添加到列表。New items are added to the list by the AddingNew event handler. 此代码示例摘自如何:自定义项添加到 Windows 窗体 BindingSource中提供的一个更大的示例。This code example is part of a larger example provided in How to: Customize Item Addition with the Windows Forms BindingSource.

// This event handler provides custom item-creation behavior.
void OnCustomersBindingSourceAddingNew(Object^ sender, 
    AddingNewEventArgs^ e)
{
    e->NewObject = DemoCustomer::CreateNewCustomer();
}
// This event handler provides custom item-creation behavior.
void customersBindingSource_AddingNew(
    object sender, 
    AddingNewEventArgs e)
{
    e.NewObject = DemoCustomer.CreateNewCustomer();
}
' This event handler provides custom item-creation behavior.
 Private Sub customersBindingSource_AddingNew( _
 ByVal sender As Object, _
 ByVal e As AddingNewEventArgs) _
 Handles customersBindingSource.AddingNew

     e.NewObject = DemoCustomer.CreateNewCustomer()

 End Sub

注解

AddingNew 事件发生在将新对象添加到由 List 属性表示的基础列表之前。The AddingNew event occurs before a new object is added to the underlying list represented by the List property. 在调用 AddNew 方法之后但在创建新项并将其添加到基础列表之前,将激发此事件。This event is fired after the AddNew method is called, but before the new item is created and added to the underlying list. 通过处理此事件,程序员可以提供自定义项的创建和插入行为,而无需强制从 BindingSource 类派生。By handling this event, the programmer can provide custom item creation and insertion behavior without being forced to derive from the BindingSource class. 在事件处理程序中,通过将 System.ComponentModel.AddingNewEventArgs 参数的 NewObject 属性设置为新项来完成此操作。This is accomplished in the event handler by setting the NewObject property of the System.ComponentModel.AddingNewEventArgs parameter to the new item. AddingNew 事件中创建的新对象的类型必须与列表中包含的类型相同,否则将发生异常。The new object created in the AddingNew event must be of the same type as the type contained in the list or an exception will occur. 当绑定到 DataViewDataTable 时,不能设置 NewObject 属性,因为无法向列表中添加新的 DataRowViewYou cannot set the NewObject property when bound to a DataView or DataTable because you cannot add a new DataRowView to the list.

有关提供自定义新项功能的详细信息,请参阅 AddNew 方法。For more information about supplying custom new item functionality, see the AddNew method. 有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅