BindingSource.AllowNew Propriedade

Definição

Obtém ou define um valor que indica se o método AddNew() pode ser usado para adicionar itens à lista.

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 da propriedade

true se AddNew() pode ser usado para adicionar itens à lista; caso contrário, false.

Exceções

Esta propriedade será definida como true quando a lista subjacente representada pela propriedade List tiver um tamanho fixo ou for somente leitura.

A propriedade será definida como true e o evento AddingNew não será tratado quando o tipo de lista subjacente não tiver um construtor sem parâmetros.

Exemplos

O exemplo de código a BindingSource seguir demonstra o AllowNew uso da propriedade do BindingSource componente para permitir que o usuário adicione novos itens à lista subjacente do componente. Definir essa propriedade true como faz com que o controle associado DataGridView exiba sua linha para novos registros.

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

Comentários

O valor padrão da AllowNew propriedade depende do tipo de fonte de dados subjacente. Se a lista subjacente implementar a IBindingList interface , essa propriedade delegará à lista subjacente. Caso contrário, essa propriedade retornará false se a lista subjacente tiver qualquer uma das seguintes características:

  • Ele tem um tamanho fixo, conforme determinado pela IList.IsFixedSize propriedade .

  • É somente leitura, conforme determinado pela IList.IsReadOnly propriedade .

  • O tipo do item não tem um construtor sem parâmetros.

Observação

Depois que o valor dessa propriedade é definido, o getter não faz mais referência à chamada para a lista subjacente. Em vez disso, ele simplesmente retorna o valor que foi definido anteriormente até que o ResetAllowNew método seja chamado.

Definir essa propriedade aciona o ListChanged evento com definido ListChangedType.Resetcomo ListChangedEventArgs.ListChangedType .

Se você definir a AllowNew propriedade como true e o tipo de lista subjacente não tiver um construtor sem parâmetros, você deverá manipular o AddingNew evento e criar o tipo apropriado.

Aplica-se a

Confira também