BindingSource.AllowNew Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy AddNew() metoda może służyć do dodawania elementów do listy.

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

Wartość właściwości

true jeśli AddNew() można użyć do dodawania elementów do listy; w przeciwnym razie false.

Wyjątki

Ta właściwość jest ustawiana na true wartość , gdy lista bazowa reprezentowana przez List właściwość ma stały rozmiar lub jest tylko do odczytu.

Właściwość jest ustawiona na true , a AddingNew zdarzenie nie jest obsługiwane, gdy podstawowy typ listy nie ma konstruktora bez parametrów.

Przykłady

Poniższy przykład kodu pokazuje użycie AllowNew właściwości BindingSource składnika, aby umożliwić użytkownikowi dodawanie nowych elementów do listy bazowej BindingSource składnika. Ustawienie tej właściwości powoduje true , że powiązana DataGridView kontrolka wyświetla wiersz dla nowych rekordów.

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

Uwagi

Wartość domyślna AllowNew właściwości zależy od bazowego typu źródła danych. Jeśli lista bazowa implementuje IBindingList interfejs, ta właściwość deleguje do bazowej listy. W przeciwnym razie ta właściwość zostanie zwrócona false , jeśli lista bazowa ma dowolną z następujących cech:

  • Ma stały rozmiar określony przez IList.IsFixedSize właściwość .

  • Jest on tylko do odczytu określony przez IList.IsReadOnly właściwość .

  • Typ elementu nie ma konstruktora bez parametrów.

Uwaga

Po ustawieniu wartości tej właściwości element getter nie odwołuje się już do wywołania źródłowej listy. Zamiast tego po prostu zwraca wartość, która została wcześniej ustawiona do momentu ResetAllowNew wywołania metody .

Ustawienie tej właściwości powoduje wywołanie ListChanged zdarzenia z ustawioną wartością ListChangedEventArgs.ListChangedTypeListChangedType.Reset.

Jeśli ustawisz AllowNew właściwość na true , a podstawowy typ listy nie ma konstruktora bez parametrów, musisz obsłużyć AddingNew zdarzenie i utworzyć odpowiedni typ.

Dotyczy

Zobacz też