BindingSource.AddingNew Event

Definition

Occurs before an item is added to the underlying list.

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

Event Type

Exceptions

NewObject is not the same type as the type contained in the list.

Examples

The following code example uses a BindingSource component to bind a list to a DataGridView control. New items are added to the list by the AddingNew event handler. 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

Remarks

The AddingNew event occurs before a new object is added to the underlying list represented by the List property. This event is fired after the AddNew method is called, but before the new item is created and added to the underlying list. By handling this event, the programmer can provide custom item creation and insertion behavior without being forced to derive from the BindingSource class. This is accomplished in the event handler by setting the NewObject property of the System.ComponentModel.AddingNewEventArgs parameter to the new item. 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. You cannot set the NewObject property when bound to a DataView or DataTable because you cannot add a new DataRowView to the list.

For more information about supplying custom new item functionality, see the AddNew method. For more information about handling events, see Handling and Raising Events.

Applies to

See also