asp.net.ph

DataTable.NewRow Method

System.Data Namespace   DataTable Class


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

[ VB ]
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

The following example initializes a DataTable, adds two DataColumn objects that determine the table's schema, and initializes 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 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.
   myDataGrid.DataSource = myDataView;
}
  C# VB

See Also

DataTable Members   AcceptChanges   Add   DataColumnCollection   DataColumn   DataRowCollection Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph