BindingSource.AllowNew Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Recebe ou define um valor que indica se o AddNew() método 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 de Propriedade
true se AddNew() pode ser usado para adicionar itens à lista; caso contrário, false.
Exceções
Esta propriedade é definida para true quando a lista subjacente representada pela List propriedade tem um tamanho fixo ou é apenas de leitura.
A propriedade é definida como true e o AddingNew evento não é tratado quando o tipo de lista subjacente não tem um construtor sem parâmetros.
Exemplos
O exemplo de código seguinte demonstra a utilização da AllowNew propriedade do BindingSource componente para permitir ao utilizador adicionar novos itens à BindingSource lista subjacente do componente. Definir esta propriedade true para faz com que o controlo associado DataGridView mostre a sua linha para novos registos.
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
Observações
O valor padrão da AllowNew propriedade depende do tipo de fonte de dados subjacente. Se a lista subjacente implementar a IBindingList interface, esta propriedade irá delegar para a lista subjacente. Caso contrário, esta propriedade devolverá false se a lista subjacente tiver alguma das seguintes características:
Tem um tamanho fixo, determinado pela IList.IsFixedSize propriedade.
É apenas de leitura, conforme determinado pela IList.IsReadOnly propriedade.
O tipo do item não tem um construtor sem parâmetros.
Note
Uma vez definido o valor desta propriedade, o getter deixa de encaminhar a chamada para a lista subjacente. Em vez disso, simplesmente devolve o valor que foi previamente definido até que o ResetAllowNew método seja chamado.
Definir esta propriedade eleva o ListChanged evento com ListChangedEventArgs.ListChangedType definido para ListChangedType.Reset.
Se definir a AllowNew propriedade como true e o tipo de lista subjacente não tiver um construtor sem parâmetros, deve tratar do AddingNew evento e criar o tipo apropriado.