BindingSource.AddNew 메서드

정의

내부 목록에 새 항목을 추가합니다.

public:
 virtual System::Object ^ AddNew();
public virtual object AddNew ();
public virtual object? AddNew ();
abstract member AddNew : unit -> obj
override this.AddNew : unit -> obj
Public Overridable Function AddNew () As Object

반환

생성되어 목록에 추가된 Object입니다.

구현

예외

AllowNew 속성은 false로 설정됩니다.

또는

현재 항목 종류에 대한 공용 매개 변수가 없는 생성자를 찾을 수 없습니다.

예제

다음 코드 예제에서는 BindingSource 구성 요소에 목록을 바인딩하는 DataGridView 제어 합니다. 새 항목으로 목록에 추가 되는 AddingNew 이벤트 처리기입니다. 이 코드 예제는에서 제공 하는 더 큰 예제의 일부입니다 방법: Windows Forms BindingSource를 사용 하 여 항목 추가 사용자 지정합니다.

private:
   
    void OnMainFormLoad(Object^ sender, EventArgs^ e)
    {
        // Add a DemoCustomer to cause a row to be displayed.
        this->customersBindingSource->AddNew();
          
        // Bind the BindingSource to the DataGridView 
        // control's DataSource.
        this->customersDataGridView->DataSource = 
            this->customersBindingSource;
    }
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    // Add a DemoCustomer to cause a row to be displayed.
    this.customersBindingSource.AddNew();

    // Bind the BindingSource to the DataGridView 
    // control's DataSource.
    this.customersDataGridView.DataSource = 
        this.customersBindingSource;
}
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)

    ' Add a DemoCustomer to cause a row to be displayed.
    Me.customersBindingSource.AddNew()

    ' Bind the BindingSource to the DataGridView 
    ' control's DataSource.
    Me.customersDataGridView.DataSource = Me.customersBindingSource

End Sub

설명

AddNew 메서드는 새 항목을 나타내는 내부 목록에 추가 된 List 속성입니다. 이 메서드는 다음과 같은 일련의 동작을 설정합니다.

  1. EndEdit 메서드 커밋 모든 보류 중인 편집 작업을 자동으로 호출 됩니다.

  2. AddingNew 이벤트가 자동으로 발생 합니다. 새 항목을 생성 하려면이 이벤트를 프로그래밍 방식으로 처리할 수 있습니다. 설정 하 여 이벤트 처리기에서 이렇게 합니다 NewObject 의 속성을 System.ComponentModel.AddingNewEventArgs 매개 변수를 새 항목. 만든 새 개체는 AddingNew 이벤트 목록에 포함 된 형식과 동일한 형식 이어야 하거나 예외가 발생 합니다.

    경우는 AddingNew 이벤트가 처리 되지 않은 및 기본 목록이 IBindingList, 요청 목록에 전달 되는 IBindingList.AddNew 메서드. 내부 목록이 IBindingList가 아닌 경우 public 매개 변수가 없는 생성자를 통해 항목이 자동으로 만들어집니다. 두 경우 모두 목록의 끝에 새 항목이 추가 됩니다.

  3. 새 항목은 목록에 추가할 즉시 내부 데이터 소스 구현 하지 않는 한를 IEditableObject 인터페이스입니다. 이 경우 새 항목 커밋되지 않습니다를 명시적으로 호출 될 때까지 ICancelAddNew.EndNew 이루어집니다 되거나 새 목록 작업을 시작 합니다. 새 항목을 호출 하 여 다시 롤백할 수 커밋되기 전에 CancelEdit, 새 항목은 삭제 하는 경우.

이 메서드는 ListChanged 이벤트입니다.

적용 대상

추가 정보