IBindingList.AddNew Method

Definition

將新的項目加入至清單中。Adds a new item to the list.

public:
 System::Object ^ AddNew();
public object AddNew ();
abstract member AddNew : unit -> obj
Public Function AddNew () As Object

Returns

Object

加入此清單的項目。The item added to the list.

Exceptions

AllowNewfalseAllowNew is false.

Remarks

只有在 trueAllowNew 時,才支援這個方法。否則,會擲回 NotSupportedExceptionThis method is supported only if AllowNew is true; otherwise, a NotSupportedException is thrown.

執行此方法表示 IBindingList 清單必須瞭解要加入清單中的物件類型,而且必須瞭解如何建立該類型的新實例。Implementing this method means that the IBindingList list must understand the type of objects to add to the list and must understand how to create a new instance of that type. 例如,如果您有 myCustomer 物件的集合,AddNew 方法應該將新的 myCustomer 物件加入至清單中。For example, if you have a collection of myCustomer objects, the AddNew method should add a new myCustomer object to the list.

Note

如果這份清單中的物件實作為 IEditableObject 介面,則在使用 AddNew 方法建立物件時,呼叫 CancelEdit 方法應該捨棄物件,而不將它加入至清單中。If the objects in this list implement the IEditableObject interface, calling the CancelEdit method should discard an object, not add it to the list, when the object was created using the AddNew method. 呼叫 IEditableObject.EndEdit 方法時,只能將物件加入至清單。The object should only be added to the list when the IEditableObject.EndEdit method is called. 因此,您必須謹慎地同步處理物件與清單。Therefore, you must synchronize the object and the list carefully.

呼叫這個方法時,您應該使用具有適當索引的 ItemAdded 列舉來引發 ListChanged 事件。When this method is called, you should raise a ListChanged event with the ItemAdded enumeration carrying the appropriate index. 加入的資料列處於狀態,在 DataGridView 控制項上叫用 Esc 可移除新的資料列。The added row is in a state where hitting Esc on a DataGridView control can remove the new row. 第二次使用 ItemAdded 列舉來引發 ListChanged 事件,表示該專案現在是一般的資料列(不是處於新狀態)。Raising the ListChanged event with the ItemAdded enumeration a second time on this row indicates that the item is now a normal row (not in new state).

Applies to