DataTable.NewRow 메서드

정의

테이블과 동일한 스키마를 갖는 새 DataRow를 만듭니다.

public:
 System::Data::DataRow ^ NewRow();
public System.Data.DataRow NewRow ();
member this.NewRow : unit -> System.Data.DataRow
Public Function NewRow () As DataRow

반환

DataRow과 동일한 스키마를 갖는 DataTable를 반환합니다.

예제

다음 예제에서는 를 만들고 DataTable테이블의 스키마를 결정하는 두 개의 DataColumn 개체를 추가하고 메서드를 사용하여 여러 개의 새 DataRow 개체를 NewRow 만듭니다. 그런 다음 메서드를 사용하여 Add 해당 DataRow 개체가 에 DataRowCollection 추가됩니다.

private void MakeDataTableAndDisplay()
{
    // Create new DataTable and DataSource objects.
    DataTable table = new DataTable();

    // Declare DataColumn and DataRow variables.
    DataColumn column;
    DataRow row;
    DataView view;

    // Create new DataColumn, set DataType, ColumnName and add to DataTable.
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.ColumnName = "id";
    table.Columns.Add(column);

    // Create second column.
    column = new DataColumn();
    column.DataType = Type.GetType("System.String");
    column.ColumnName = "item";
    table.Columns.Add(column);

    // Create new DataRow objects and add to DataTable.
    for(int i = 0; i < 10; i++)
    {
        row = table.NewRow();
        row["id"] = i;
        row["item"] = "item " + i.ToString();
        table.Rows.Add(row);
    }

    // Create a DataView using the DataTable.
    view = new DataView(table);

    // Set a DataGrid control's DataSource to the DataView.
    dataGrid1.DataSource = view;
}
Private Sub MakeDataTableAndDisplay()
    ' Create new DataTable and DataSource objects.
    Dim table As New DataTable()

    ' Declare DataColumn and DataRow variables.
    Dim column As DataColumn 
    Dim row As DataRow 
    Dim view As DataView 

    ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
    column = New DataColumn()
    column.DataType = System.Type.GetType("System.Int32")
    column.ColumnName = "id"
    table.Columns.Add(column)
 
    ' Create second column.
    column = New DataColumn()
    column.DataType = Type.GetType("System.String")
    column.ColumnName = "item"
    table.Columns.Add(column)
 
    ' Create new DataRow objects and add to DataTable.    
    Dim i As Integer
    For i = 0 to 9 
       row = table.NewRow()
       row("id") = i
       row("item") = "item " & i
       table.Rows.Add(row)
    Next
    ' Create a DataView using the DataTable.
    view = New DataView(table)

    ' Set a DataGrid control's DataSource to the DataView.
    DataGrid1.DataSource = view
End Sub

설명

메서드를 NewRow 사용하여 와 동일한 스키마를 사용하여 새 DataRow 개체를 DataTable만들어야 합니다. 를 DataRow만든 후 개체의 Rows 속성을 통해 DataTableDataRowCollection추가할 수 있습니다. 를 사용하여 NewRow 새 행을 만들 때 를 호출 Clear하기 전에 데이터 테이블에 행을 추가하거나 데이터 테이블에서 삭제해야 합니다.

적용 대상

추가 정보