Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataTable.NewRow Method

Creates a new DataRow with the same schema as the table.

[Visual Basic]
Public Function NewRow() As DataRow
[C#]
public DataRow NewRow();
[C++]
public: DataRow* NewRow();
[JScript]
public function NewRow() : DataRow;

Return Value

A DataRow with the same schema as the DataTable.

Remarks

You must use the NewRow method to create new DataRow objects with the same schema as the DataTable. After creating a DataRow, you can add it to the DataRowCollection, through the DataTable object's Rows property.

Example

[Visual Basic, C#, C++] The following example creates a DataTable, adds two DataColumn objects that determine the table's schema, and creates several new DataRow objects using the NewRow method. Those DataRow objects are then added to the DataRowCollection using the Add method.

[Visual Basic] 
Private Sub MakeDataTableAndDisplay()
   ' Create new DataTable and DataSource objects.
   Dim myDataTable As DataTable = New DataTable()
   ' Declare DataColumn and DataRow variables.
   Dim myColumn As DataColumn 
   Dim myRow As DataRow 
   Dim myDataView As DataView 
   ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
   myColumn = New DataColumn()
   myColumn.DataType = System.Type.GetType("System.Int32")
   myColumn.ColumnName = "id"
   myDataTable.Columns.Add(myColumn)

   ' Create second column.
   myColumn = New DataColumn()
   myColumn.DataType = Type.GetType("System.String")
   myColumn.ColumnName = "item"
   myDataTable.Columns.Add(myColumn)

   ' Create new DataRow objects and add to DataTable.    
   Dim i As Integer
   For i = 0 to 9 
      myRow = myDataTable.NewRow()
      myRow("id") = i
      myRow("item") = "item " & i
      myDataTable.Rows.Add(myRow)
   Next
   ' Create a DataView using the DataTable.
   myDataView = New DataView(myDataTable)
   ' Set a DataGrid control's DataSource to the DataView.
   DataGrid1.DataSource = myDataView
End Sub

[C#] 
private void MakeDataTableAndDisplay(){
   // Create new DataTable and DataSource objects.
   DataTable myDataTable = new DataTable();
   // Declare DataColumn and DataRow variables.
   DataColumn myColumn;
   DataRow myRow; 
   DataView myDataView;
   // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
   myColumn = new DataColumn();
   myColumn.DataType = System.Type.GetType("System.Int32");
   myColumn.ColumnName = "id";
   myDataTable.Columns.Add(myColumn);

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

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

   // Create a DataView using the DataTable.
   myDataView = new DataView(myDataTable);
   // Set a DataGrid control's DataSource to the DataView.
   dataGrid1.DataSource = myDataView;
}

[C++] 
private:
 void MakeDataTableAndDisplay(){
    // Create new DataTable and DataSource objects.
    DataTable* myDataTable = new DataTable();
    // Declare DataColumn and DataRow variables.
    DataColumn* myColumn;
    DataRow* myRow; 
    DataView* myDataView;
    // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
    myColumn = new DataColumn();
    myColumn->DataType = System::Type::GetType(S"System.Int32");
    myColumn->ColumnName = S"id";
    myDataTable->Columns->Add(myColumn);
 
    // Create second column.
    myColumn = new DataColumn();
    myColumn->DataType = Type::GetType(S"System.String");
    myColumn->ColumnName = S"item";
    myDataTable->Columns->Add(myColumn);
 
    // Create new DataRow objects and add to DataTable.    
    for(int i = 0; i < 10; i++){
       myRow = myDataTable->NewRow();
       myRow->Item[S"id"] = __box(i);
       myRow->Item[S"item"] = String::Format( S"item {0}", __box(i));
       myDataTable->Rows->Add(myRow);
    }
 
    // Create a DataView using the DataTable.
    myDataView = new DataView(myDataTable);
    // Set a DataGrid control's DataSource to the DataView.
    dataGrid1->DataSource = myDataView;
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTable Class | DataTable Members | System.Data Namespace | AcceptChanges | Add | DataColumnCollection | DataColumn | DataRowCollection

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft