BindingSource.AddingNew 事件


在項目加入基礎清單之前發生。Occurs before an item is added to the underlying list.

 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. 這個程式碼範例是 how to:使用 Windows Forms 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 屬性,因為您無法將新的 DataRowView 加入清單中。You 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.