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 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

備註

事件發生于新的物件加入至List屬性所表示的基礎清單之前。 AddingNewThe 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. 這是在事件處理常式中完成, 方法NewObject是將System.ComponentModel.AddingNewEventArgs參數的屬性設定為新的專案。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. 您無法在系NewObjectDataView至或DataTable時設定屬性, 因為您無法將新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.

適用於

另請參閱