Uredi

Deli z drugimi prek


BindingSource.AddNew Method

Definition

Adds a new item to the underlying list.

public:
 virtual System::Object ^ AddNew();
public virtual object AddNew ();
public virtual object? AddNew ();
abstract member AddNew : unit -> obj
override this.AddNew : unit -> obj
Public Overridable Function AddNew () As Object

Returns

The Object that was created and added to the list.

Implements

Exceptions

The AllowNew property is set to false.

-or-

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

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.

private:
   
    void OnMainFormLoad(Object^ sender, EventArgs^ e)
    {
        // Add a DemoCustomer to cause a row to be displayed.
        this->customersBindingSource->AddNew();
          
        // Bind the BindingSource to the DataGridView 
        // control's DataSource.
        this->customersDataGridView->DataSource = 
            this->customersBindingSource;
    }
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    // Add a DemoCustomer to cause a row to be displayed.
    this.customersBindingSource.AddNew();

    // Bind the BindingSource to the DataGridView 
    // control's DataSource.
    this.customersDataGridView.DataSource = 
        this.customersBindingSource;
}
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)

    ' Add a DemoCustomer to cause a row to be displayed.
    Me.customersBindingSource.AddNew()

    ' Bind the BindingSource to the DataGridView 
    ' control's DataSource.
    Me.customersDataGridView.DataSource = Me.customersBindingSource

End Sub

Remarks

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

Applies to

See also