DataTable.NewRow Method
Assembly: System.Data (in system.data.dll)
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.
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" 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
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.