BindingSource.AllowNew Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 Overridable Property AllowNew As Boolean

Property Value

Type: System.Boolean

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

Exception Condition
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.

System_CAPS_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 Sub New() 
    ' Set up the form.
    Me.Size = New Size(800, 800)
    AddHandler Me.Load, AddressOf Form1_Load

    ' Set up the DataGridView control.
    Me.customersDataGridView.AllowUserToAddRows = True
    Me.customersDataGridView.Dock = DockStyle.Fill
    Me.Controls.Add(customersDataGridView)

    ' Add the StatusBar control to the form.
    Me.Controls.Add(status)

    ' Allow the user to add new items.
    Me.customersBindingSource.AllowNew = True

    ' Attach the BindingSource to the DataGridView.
    Me.customersDataGridView.DataSource = Me.customersBindingSource

End Sub

.NET Framework
Available since 2.0
Return to top
Show: