Partager via


BindingSource.AllowNew Propriété

Définition

Obtient ou définit une valeur indiquant si la AddNew() méthode peut être utilisée pour ajouter des éléments à la liste.

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

Valeur de propriété

true si AddNew() vous pouvez l’utiliser pour ajouter des éléments à la liste ; sinon, false.

Exceptions

Cette propriété est définie true lorsque la liste sous-jacente représentée par la List propriété a une taille fixe ou est en lecture seule.

La propriété est définie true et l’événement AddingNew n’est pas géré lorsque le type de liste sous-jacent n’a pas de constructeur sans paramètre.

Exemples

L’exemple de code suivant illustre l’utilisation de la AllowNew propriété du BindingSource composant pour permettre à l’utilisateur d’ajouter de nouveaux éléments à la BindingSource liste sous-jacente du composant. Si vous définissez cette propriété pour true que le contrôle lié DataGridView affiche sa ligne pour les nouveaux enregistrements.

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

Remarques

La valeur par défaut de la AllowNew propriété dépend du type de source de données sous-jacent. Si la liste sous-jacente implémente l’interface IBindingList , cette propriété déléguera à la liste sous-jacente. Sinon, cette propriété retourne false si la liste sous-jacente présente l’une des caractéristiques suivantes :

  • Elle a une taille fixe, telle que déterminée par la IList.IsFixedSize propriété.

  • Il est en lecture seule, tel que déterminé par la IList.IsReadOnly propriété.

  • Le type de l’élément n’a pas de constructeur sans paramètre.

Note

Une fois la valeur de cette propriété définie, le getter ne fait plus référence à l’appel à la liste sous-jacente. Au lieu de cela, il retourne simplement la valeur qui a été précédemment définie jusqu’à ce que la ResetAllowNew méthode soit appelée.

La définition de cette propriété déclenche l’événement ListChanged avec ListChangedEventArgs.ListChangedType la valeur ListChangedType.Resetdéfinie sur .

Si vous définissez la AllowNew propriété sur true et que le type de liste sous-jacent n’a pas de constructeur sans paramètre, vous devez gérer l’événement AddingNew et créer le type approprié.

S’applique à

Voir aussi