共用方式為


BindingSource.AllowNew 屬性

定義

取得或設定值,指出 AddNew() 方法是否可以用來將項目加入清單中。

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

屬性值

如果 AddNew() 可以用來將項目加入清單中,則為 true,否則為 false

例外狀況

當由 List 屬性表示的基礎清單有固定大小或者是唯讀的,這個屬性就會設定為 true

這個屬性會設定為 true,且當基礎清單類型沒有無參數建構函式時,將不會處理 AddingNew 事件。

範例

下列程式碼範例示範如何使用 AllowNew 元件的 屬性 BindingSource ,允許使用者將新專案新增至 BindingSource 元件的基礎清單。 將此屬性設定為 true 會導致系結 DataGridView 控制項顯示新記錄的資料列。

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

備註

屬性的 AllowNew 預設值取決於基礎資料來源類型。 如果基礎清單實作 IBindingList 介面,這個屬性會委派至基礎清單。 否則,如果基礎清單具有下列任何特性,則此屬性會傳回 false

  • 它具有固定大小,由 屬性決定 IList.IsFixedSize

  • 它是唯讀的,由 屬性決定 IList.IsReadOnly

  • 專案的型別沒有無參數建構函式。

注意

設定此屬性的值之後,getter 就不會再參考基礎清單的呼叫。 相反地,它只會傳回先前設定的值,直到 ResetAllowNew 呼叫 方法為止。

設定這個屬性會 ListChanged 引發設定為 ListChangedType.Reset 的事件 ListChangedEventArgs.ListChangedType

如果您將 AllowNew 屬性設定為 true ,且基礎清單類型沒有無參數建構函式,則必須處理 AddingNew 事件並建立適當的類型。

適用於

另請參閱