Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
BindingSource Class
 AddingNew Event
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
BindingSource..::.AddingNew Event

Updated: November 2007

Occurs before an item is added to the underlying list.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Visual Basic (Declaration)
Public Event AddingNew As AddingNewEventHandler
Visual Basic (Usage)
Dim instance As BindingSource
Dim handler As AddingNewEventHandler

AddHandler instance.AddingNew, handler
C#
public event AddingNewEventHandler AddingNew
Visual C++
public:
 event AddingNewEventHandler^ AddingNew {
    void add (AddingNewEventHandler^ value);
    void remove (AddingNewEventHandler^ value);
}
J#
/** @event */
public void add_AddingNew (AddingNewEventHandler value)
/** @event */
public void remove_AddingNew (AddingNewEventHandler value)
JScript
JScript does not support events.
ExceptionCondition
InvalidOperationException

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

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 Consuming Events.

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.

Visual Basic
' 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

C#
// This event handler provides custom item-creation behavior.
void customersBindingSource_AddingNew(
    object sender, 
    AddingNewEventArgs e)
{
    e.NewObject = DemoCustomer.CreateNewCustomer();
}

Visual C++
// This event handler provides custom item-creation behavior.
void OnCustomersBindingSourceAddingNew(Object^ sender, 
    AddingNewEventArgs^ e)
{
    e->NewObject = DemoCustomer::CreateNewCustomer();
}

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker