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.

调用此方法时,应引发 ListChanged 事件,其中包含具有适当索引的 ItemAdded 枚举。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