Export (0) Print
Expand All

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 DataRow NewRow ()
public DataRow NewRow ()
public function NewRow () : DataRow

Return Value

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.

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 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;


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.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft