DataRowCollection.Add 메서드

정의

DataRowDataRowCollection를 추가합니다.

오버로드

Add(DataRow)

지정된 DataRowDataRowCollection 개체에 추가합니다.

Add(Object[])

지정한 값을 사용하여 행을 만들고 그 행을 DataRowCollection에 추가합니다.

Add(DataRow)

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

지정된 DataRowDataRowCollection 개체에 추가합니다.

public:
 void Add(System::Data::DataRow ^ row);
public void Add (System.Data.DataRow row);
member this.Add : System.Data.DataRow -> unit
Public Sub Add (row As DataRow)

매개 변수

row
DataRow

추가할 DataRow입니다.

예외

행이 null인 경우

이 행이 다른 테이블에 속하거나 이 테이블에 이미 속해 있는 경우

추가로 인해 제약 조건이 무효화되는경우

추가하는 동안 DataColumn이 false인 AllowDBNull에 null을 삽입하려고 하는 경우

예제

다음 예제에서는 메서드를 Add 사용하여 개체에 새 DataRowDataRowCollection 추가합니다.

private void ShowRows(DataTable table)
{
    // Print the number of rows in the collection.
    Console.WriteLine(table.Rows.Count);
    // Print the value of columns 1 in each row
    foreach(DataRow row in table.Rows)
    {
        Console.WriteLine(row[1]);
    }
}

private void AddRow(DataTable table)
{
    DataRowCollection rowCollection = table.Rows;
    // Instantiate a new row using the NewRow method.

    DataRow newRow = table.NewRow();
    // Insert code to fill the row with values.

    // Add the row to the DataRowCollection.
    table.Rows.Add(newRow);
}
Private Sub ShowRows(Byval table As DataTable)
    ' Print the number of rows in the collection.
    Console.WriteLine(table.Rows.Count)

    Dim row  As DataRow
    ' Print the value of columns 1 in each row
    For Each row In table.Rows
        Console.WriteLine(row(1))
    Next
End Sub
 
Private Sub AddRow(ByVal table As DataTable)
    ' Instantiate a new row using the NewRow method.
    Dim newRow As DataRow = table.NewRow()
    ' Insert code to fill the row with values.

    ' Add the row to the DataRowCollection.
    table.Rows.Add(newRow)
End Sub

설명

DataRow를 만들려면 클래스의 메서드를 NewRowDataTable 사용해야 합니다. 메서드를 NewRow 사용하면 부모 DataTable의 스키마를 사용하여 새 DataRow 개체가 반환됩니다. 개체를 DataRow 만들고 각 열에 대한 값을 설정한 후 메서드를 Add 사용하여 개체를 컬렉션에 추가합니다.

사용자가 이벤트에서 예외를 생성하는 경우 예외를 RowChanging 생성합니다. 예외가 발생하면 행이 테이블에 추가되지 않습니다.

추가 정보

적용 대상

Add(Object[])

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

지정한 값을 사용하여 행을 만들고 그 행을 DataRowCollection에 추가합니다.

public:
 System::Data::DataRow ^ Add(... cli::array <System::Object ^> ^ values);
public:
 virtual System::Data::DataRow ^ Add(cli::array <System::Object ^> ^ values);
public System.Data.DataRow Add (params object?[] values);
public System.Data.DataRow Add (params object[] values);
public virtual System.Data.DataRow Add (object[] values);
member this.Add : obj[] -> System.Data.DataRow
abstract member Add : obj[] -> System.Data.DataRow
override this.Add : obj[] -> System.Data.DataRow
Public Function Add (ParamArray values As Object()) As DataRow
Public Overridable Function Add (values As Object()) As DataRow

매개 변수

values
Object[]

새 행을 만드는데 사용하는 값 배열입니다.

반환

새 행입니다.

예외

배열이 테이블의 열 수보다 큰 경우

값이 해당 열 형식과 일치하지 않는 경우

행을 추가하여 제약 조건을 무효화한 경우

AllowDBNull이 false인 열에 null을 설정하려고 한 경우

예제

다음 예제에서는 메서드를 Add 사용하여 새 DataRow 개체를 만들고 에 DataRowCollection추가합니다.

private void AddRow(DataTable table)
{
    // Create an array with three elements.
    object[] rowVals = new object[3];
    DataRowCollection rowCollection = table.Rows;
    rowVals[0] = "hello";
    rowVals[1] = "world";
    rowVals[2] = "two";

    // Add and return the new row.
    DataRow row = rowCollection.Add(rowVals);
}
Private Sub AddRow(ByVal table As DataTable)
    ' Create an array with three elements.
    Dim rowVals(2) As Object
    Dim rowCollection As DataRowCollection = table.Rows
    rowVals(0) = "hello"
    rowVals(1) = "world"
    rowVals(2) = "two"

    ' Add and return the new row.
    Dim row As DataRow = rowCollection.Add(rowVals) 
End Sub

설명

개체 AutoIncrementDataColumn True로 설정된 경우 해당 열의 기본값을 얻으려면 null을 전달해야 합니다.

또는 RowChanging 이벤트 중에 예외를 생성하는 경우에도 예외가 ColumnChanging 발생할 수 있습니다. 예외가 발생하면 행이 테이블에 추가되지 않습니다.

추가 정보

적용 대상