0 out of 1 rated this helpful - Rate this topic

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()

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 added to or deleted from the data table 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 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;
}


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Help: Dim row As DataRow
Hi All

I've a bit problem with my coding. What i m trying to do is to add a row in a table, but i m having errors.
When i use
Dim row as Datarow
row = ds.Tables("Administrator").Rows.Add()
I m having error "Object reference not set to an instance of an object."
then i use
Dim row as New datarow
i m having error "Error    1    'System.Data.DataRow.Protected Friend Sub New(builder As System.Data.DataRowBuilder)' is not accessible in this context because it is 'Protected Friend'.    C:\Documents and Settings\Faraz Khan\my documents\visual studio 2010\Projects\RJ\RJ\Login.vb    103    21    RJ
"

PLease help me with this. m not able to understand. I tried different forums too. but failed.
Data binding is missing
$0The last line after the :$0 $0$0 $0 $0datagrid1..DataSource = view;$0 $0$0 $0 $0should be :$0 $0$0 $0 datagrid1.DataBind(); $0$0 $0 $0in order for the data grid to show the populated data in the table format on your web page.$0
  • 5/25/2011
  • K Z
NewRow Method
I am new with .Net programming.
I am using Tableadapters and wants to add new row in the single table. I am using NewRow(); but its giving problem.
Do we need to add any other namespace than system.data for NewRow? Is this NewRow built in method ?

Thanks for help..