Export (0) Print
Expand All

BindingSource.AllowNew Property

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

Gets or sets a value indicating whether the AddNew method can be used to add items to the list.

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

public virtual bool AllowNew { get; set; }
/** @property */
public boolean get_AllowNew ()

/** @property */
public void set_AllowNew (boolean value)

public function get AllowNew () : boolean

public function set AllowNew (value : boolean)

Property Value

true if AddNew can be used to add items to the list; otherwise, false.

Exception typeCondition

InvalidOperationException

This property is set to true when the underlying list represented by the List property has a fixed size or is read-only.

MissingMethodException

The property is set to true and the AddingNew event is not handled when the underlying list type does not have a default constructor.

The default value for the AllowNew property depends on the underlying data source type. If the underlying list implements the IBindingList interface, this property will delegate to the underlying list. Otherwise, this property will return false if the underlying list has any of the following characteristics:

  • It has a fixed size, as determined by the IList.IsFixedSize property.

  • It is read-only, as determined by the IList.IsReadOnly property.

  • The item's type does not have a default constructor.

NoteNote

Once the value of this property is set, the getter no longer refers the call to the underlying list. Instead, it simply returns the value that was previously set until the ResetAllowNew method is called.

Setting this property raises the ListChanged event with ListChangedEventArgs.ListChangedType set to ListChangedType.Reset.

If you set the AllowNew property to true and the underlying list type does not have a default constructor, you must handle the AddingNew event and create the appropriate type.

The following code example demonstrates using the AllowNew property of the BindingSource component to permit the user to add new items to the BindingSource component's underlying list. Setting this property to true causes the bound DataGridView control to display its row for new records.

public Form1()
{
    // Set up the form.
    this.Size = new Size(800, 800);
    this.Load += new EventHandler(Form1_Load);

    // Set up the DataGridView control.
    this.customersDataGridView.AllowUserToAddRows = true;
    this.customersDataGridView.Dock = DockStyle.Fill;
    this.Controls.Add(customersDataGridView);

    // Add the StatusBar control to the form.
    this.Controls.Add(status);

    // Allow the user to add new items.
    this.customersBindingSource.AllowNew = true;

    // Attach an event handler for the AddingNew event.
    this.customersBindingSource.AddingNew +=
        new AddingNewEventHandler(customersBindingSource_AddingNew);

    // Attach an eventhandler for the ListChanged event.
    this.customersBindingSource.ListChanged +=
        new ListChangedEventHandler(customersBindingSource_ListChanged);

    
    // Attach the BindingSource to the DataGridView.
    this.customersDataGridView.DataSource =
        this.customersBindingSource;
}

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:
© 2014 Microsoft