Condividi tramite


BindingSource.AllowNew Proprietà

Definizione

Ottiene o imposta un valore che indica se il AddNew() metodo può essere utilizzato per aggiungere elementi all'elenco.

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

Valore della proprietà

true se AddNew() può essere utilizzato per aggiungere elementi all'elenco; in caso contrario, false.

Eccezioni

Questa proprietà viene impostata su true quando l'elenco sottostante rappresentato dalla List proprietà ha una dimensione fissa o è di sola lettura.

La proprietà è impostata su true e l'evento AddingNew non viene gestito quando il tipo di elenco sottostante non dispone di un costruttore senza parametri.

Esempio

Nell'esempio di codice seguente viene illustrata l'utilizzo della AllowNew proprietà del BindingSource componente per consentire all'utente di aggiungere nuovi elementi all'elenco BindingSource sottostante del componente. Se si imposta questa proprietà su true , il controllo associato DataGridView visualizza la relativa riga per i nuovi record.

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

Commenti

Il valore predefinito per la AllowNew proprietà dipende dal tipo di origine dati sottostante. Se l'elenco sottostante implementa l'interfaccia IBindingList , questa proprietà delega all'elenco sottostante. In caso contrario, questa proprietà restituirà false se l'elenco sottostante presenta una delle caratteristiche seguenti:

  • Ha una dimensione fissa, come determinato dalla IList.IsFixedSize proprietà .

  • È di sola lettura, come determinato dalla IList.IsReadOnly proprietà .

  • Il tipo dell'elemento non dispone di un costruttore senza parametri.

Annotazioni

Dopo aver impostato il valore di questa proprietà, il getter non fa più riferimento alla chiamata all'elenco sottostante. Restituisce invece semplicemente il valore impostato in precedenza fino a quando non viene chiamato il ResetAllowNew metodo .

L'impostazione di questa proprietà genera l'evento ListChanged con ListChangedEventArgs.ListChangedType impostato su ListChangedType.Reset.

Se si imposta la AllowNew proprietà su true e il tipo di elenco sottostante non dispone di un costruttore senza parametri, è necessario gestire l'evento AddingNew e creare il tipo appropriato.

Si applica a

Vedi anche