BindingSource.AllowNew Egenskap

Definition

Hämtar eller anger ett värde som anger om AddNew() metoden kan användas för att lägga till objekt i listan.

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

Egenskapsvärde

trueom AddNew() kan användas för att lägga till objekt i listan, annars . false

Undantag

Den här egenskapen anges till true när den underliggande listan som representeras av List egenskapen har en fast storlek eller är skrivskyddad.

Egenskapen är inställd på true och AddingNew händelsen hanteras inte när den underliggande listtypen inte har någon parameterlös konstruktor.

Exempel

Följande kodexempel visar hur du använder AllowNew egenskapen för komponenten BindingSource för att tillåta användaren att lägga till nya objekt i komponentens BindingSource underliggande lista. Om du ställer in den här egenskapen så true visas den bundna DataGridView kontrollen för nya poster.

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

Kommentarer

Standardvärdet för AllowNew egenskapen beror på den underliggande datakälltypen. Om den underliggande listan implementerar gränssnittet delegeras IBindingList den här egenskapen till den underliggande listan. Annars returneras false den här egenskapen om den underliggande listan har någon av följande egenskaper:

  • Den har en fast storlek som bestäms av egenskapen IList.IsFixedSize .

  • Den är skrivskyddad, vilket bestäms av IList.IsReadOnly egenskapen.

  • Objektets typ har ingen parameterlös konstruktor.

Note

När värdet för den här egenskapen har angetts refererar getter inte längre anropet till den underliggande listan. I stället returnerar den helt enkelt det värde som tidigare angavs tills ResetAllowNew metoden anropas.

Om du anger den här egenskapen genereras ListChanged händelsen med ListChangedEventArgs.ListChangedType inställt på ListChangedType.Reset.

Om du anger AllowNew egenskapen till true och den underliggande listtypen inte har någon parameterlös konstruktor måste du hantera AddingNew händelsen och skapa lämplig typ.

Gäller för

Se även