This documentation is archived and is not being maintained.

DataTable.NewRow Method

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

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

Public Function NewRow As DataRow
Dim instance As DataTable 
Dim returnValue As DataRow 

returnValue = instance.NewRow()

Return Value

Type: System.Data.DataRow
A DataRow with the same schema as the DataTable.

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. When you use NewRow to create new rows, the rows must be processed before you call Clear.

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.

Private Sub MakeDataTableAndDisplay()
    ' Create new DataTable and DataSource objects. 
    Dim table As DataTable = 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"

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

    ' 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
    ' Create a DataView using the DataTable.
    view = New DataView(table)

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0