Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

BindingSource.AddNew Method

Note: This method is new in the .NET Framework version 2.0.

Adds a new item to the underlying list.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public virtual Object AddNew ()
public Object AddNew ()
public function AddNew () : Object

Return Value

The Object that was created and added to the list.

Exception typeCondition

InvalidOperationException

The AllowNew property is set to false.

-or-

A public default constructor could not be found for the current item type.

The AddNew method adds a new item to the underlying list represented by the List property. This method sets up the following series of actions:

  1. The EndEdit method is automatically called to commit any pending edit operations.

  2. The AddingNew event is automatically raised. This event can be programmatically handled to construct the new item. 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.

    If the AddingNew event is not handled, and the underlying list is an IBindingList, then the request is passed to the list's IBindingList.AddNew method. If the underlying list is not an IBindingList, the item is automatically created through its public default constructor. In either case, the new item is added to the end of the list.

  3. The new item is added immediately to the internal list unless the data source implements the IEditableObject interface. In this case, the new item is not committed until an explicit call to System.ComponentModel.ICancelAddNew.EndNew is made or until a new list operation is initiated. Before it is committed, the new item can be rolled back by calling CancelEdit, in which case the new item is discarded.

This method raises the ListChanged event.

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 customersBindingSource_AddingNew(
    object sender, 
    AddingNewEventArgs e)
{
    e.NewObject = DemoCustomer.CreateNewCustomer();
}

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

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft