BindingSource.AllowNew BindingSource.AllowNew BindingSource.AllowNew BindingSource.AllowNew Property

Definición

Obtiene o establece un valor que indica si el método AddNew() se puede utilizar para agregar elementos a la lista.Gets or sets a value indicating whether the AddNew() method can be used to add items to the list.

public:
 virtual property bool AllowNew { bool get(); void set(bool value); };
public virtual bool AllowNew { get; set; }
member this.AllowNew : bool with get, set
Public Overridable Property AllowNew As Boolean

Valor de propiedad

true si se puede utilizar AddNew() para agregar elementos a la lista; en caso contrario, devuelve false.true if AddNew() can be used to add items to the list; otherwise, false.

Excepciones

Esta propiedad se establece en true cuando la lista subyacente representada por la propiedad List tiene un tamaño fijo o es de sólo lectura.This property is set to true when the underlying list represented by the List property has a fixed size or is read-only.

La propiedad se establece en true y no se controla el evento AddingNew cuando el tipo de lista subyacente no tiene ningún constructor sin parámetros.The property is set to true and the AddingNew event is not handled when the underlying list type does not have a parameterless constructor.

Ejemplos

En el ejemplo BindingSource de código siguiente se AllowNew muestra cómo utilizar la propiedad del componente para permitir al usuario agregar nuevos elementos BindingSource a la lista subyacente del componente.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. Al establecer esta propiedad true en, el DataGridView control enlazado muestra su fila para los nuevos registros.Setting this property to true causes the bound DataGridView control to display its row for new records.

Form1()
{
   // Set up the form.
   this->Size = System::Drawing::Size( 800, 800 );
   this->Load += gcnew EventHandler( this, &Form1::Form1_Load );
   
   // Set up the RadioButton controls.
   this->allRadioBtn->Text = L"All";
   this->allRadioBtn->Checked = true;
   this->allRadioBtn->CheckedChanged += gcnew EventHandler(
      this, &Form1::allRadioBtn_CheckedChanged );
   this->allRadioBtn->Dock = DockStyle::Top;
   this->currentRadioBtn->Text = L"Current";
   this->currentRadioBtn->CheckedChanged += gcnew EventHandler(
      this, &Form1::currentRadioBtn_CheckedChanged );
   this->currentRadioBtn->Dock = DockStyle::Top;
   this->noneRadioBtn->Text = L"None";
   this->noneRadioBtn->CheckedChanged += gcnew EventHandler(
      this, &Form1::noneRadioBtn_CheckedChanged );
   this->noneRadioBtn->Dock = DockStyle::Top;
   this->buttonPanel->Controls->Add( this->allRadioBtn );
   this->buttonPanel->Controls->Add( this->currentRadioBtn );
   this->buttonPanel->Controls->Add( this->noneRadioBtn );
   this->buttonPanel->Dock = DockStyle::Bottom;
   this->Controls->Add( this->buttonPanel );
   
   // 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 +=
      gcnew AddingNewEventHandler(
         this, &Form1::customersBindingSource_AddingNew );
   
   // Attach an eventhandler for the ListChanged event.
   this->customersBindingSource->ListChanged +=
      gcnew ListChangedEventHandler(
         this, &Form1::customersBindingSource_ListChanged );
   
   // Set the initial value of the ItemChangedEventMode property
   // to report all ListChanged events.
   this->customersBindingSource->ItemChangedEventMode = 
     ItemChangedEventMode::All;
   
   // Attach the BindingSource to the DataGridView.
   this->customersDataGridView->DataSource =
      this->customersBindingSource;
}
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;
}
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

Comentarios

El valor predeterminado de la AllowNew propiedad depende del tipo de origen de datos subyacente.The default value for the AllowNew property depends on the underlying data source type. Si la lista subyacente implementa la IBindingList interfaz, esta propiedad delegará en la lista subyacente.If the underlying list implements the IBindingList interface, this property will delegate to the underlying list. De lo contrario, esta propiedad false devolverá si la lista subyacente tiene alguna de las siguientes características:Otherwise, this property will return false if the underlying list has any of the following characteristics:

  • Tiene un tamaño fijo, determinado por la IList.IsFixedSize propiedad.It has a fixed size, as determined by the IList.IsFixedSize property.

  • Es de solo lectura, según lo determinado por la IList.IsReadOnly propiedad.It is read-only, as determined by the IList.IsReadOnly property.

  • El tipo del elemento no tiene un constructor sin parámetros.The item's type does not have a parameterless constructor.

Nota

Una vez establecido el valor de esta propiedad, el captador ya no hace referencia a la llamada a la lista subyacente.Once the value of this property is set, the getter no longer refers the call to the underlying list. En su lugar, simplemente devuelve el valor que se estableció previamente hasta que ResetAllowNew se llama al método.Instead, it simply returns the value that was previously set until the ResetAllowNew method is called.

Al establecer esta propiedad, ListChanged se genera ListChangedEventArgs.ListChangedType el evento ListChangedType.Resetcon establecido en.Setting this property raises the ListChanged event with ListChangedEventArgs.ListChangedType set to ListChangedType.Reset.

Si establece la AllowNew propiedad en true y el tipo de lista subyacente no tiene un constructor sin parámetros, debe controlar el AddingNew evento y crear el tipo adecuado.If you set the AllowNew property to true and the underlying list type does not have a parameterless constructor, you must handle the AddingNew event and create the appropriate type.

Se aplica a

Consulte también: