.NET Framework class library
DataTable.newrow method
Create a new DataRow with the table with the same architecture.
[Visual Basic]
Public function new () AS DATAROW
[C #]
Public DataRow newRow ();
[C ]
PUBLIC: DATAROW * new ();
[Jscript]
PUBLIC FUNCTION NEWROW (): DATAROW;
return value
DataRow, its architecture is the same as the DataTable architecture.
Note
The new DataRow object with the DataTable with the new DataBole must be created using the new path method. After creating DATAROW, you can add it to the DataRowCollection via the DataTable object's ROWS attribute.
Example
[Visual Basic, C #, C ] The following example creates a DataTable, add two DataColumn objects for determining the schema of the table, and create a few new DataRow objects using the Newrow method. Then add these DATAROW objects to the DataRowCollection using the Add method.
[Visual Basic]
Private sub makeDataTableAndDisplay ()
'CREATE New DataTable and DataSource Objects.
Dim mydataable as datatable = new data ()
'Declare Datacolumn and DataRow Variables.
DIM MyColumn As Datacolumn
Dim Myrow As DataRow
Dim MyDataView As DataView
'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.
DIM I as integer
For i = 0 TO 9
Myrow = mydataable.newrow ()
Myrow ("id") = i
Myrow ("item") = "Item" & i
MyDataTable.Rows.Add (Myrow)
NEXT
'Create a dataview using the dataable.
MyDataView = New DataView (MyDataTable)
'Set a DataGrid Control's DataSource to the dataview.
DataGrid1.datasource = MyDataViewend Sub
[C #]
Private void makedataableAnddisplay () {
// CREATE New DataTable and DataSource Objects.
DataTable myDataTable = new dataatable ();
// Declare Datacolumn and DataRow Variables.
Datacolumn mycolumn;
DataRow myrow;
DataView mydataview;
// Create New Datacolumn, Set DataType, ColumnName and Add to DataBole.
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 dataable.
For (int i = 0; i <10; i ) {
Myrow = mydataable.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.
DataGrid1.datasource = MyDataView;
}
[C ]
Private:
Void makeDataTableAndDisplay () {
// CREATE New DataTable and DataSource Objects.
DataTable * mydataable = new data ();
// Declare Datacolumn and DataRow Variables.
Datacolumn * mycolumn;
DataRow * Myrow;
DataView * MyDataView;
// Create New Datacolumn, Set DataType, ColumnName and Add to DataBole.
Mycolumn = new datacolumn ();
MyColumn-> DataType = system :: type :: gettype (s "System.Int32");
mycolumn-> columnname = s "id";
MyDataTable-> Column-> Add (mycolumn);
// Create Second Column.mycolumn = New Datacolumn ();
MyColumn-> DataType = type :: gettype (s "system.sw");
mycolumn-> columnname = s "item";
MyDataTable-> Column-> Add (mycolumn);
// CREATE New DataRow Objects and add to dataable.
For (int i = 0; i <10; i ) {
Myrow = mydataable-> new();
Myrow-> item [s "id"] = __box (i);
Myrow-> item [s "item"] = String :: format (s "item {0}", __box (i));
MyDataTable-> rows-> add (myrow);
}
// CREATE A DATAVIEW Using The DataTable.
MyDataView = New DataView (MyDataTable);
// set a DataGrid Control's DataSource to the dataview.
DataGrid1-> DataSource = MyDataView;
}
[JScript] There is no example available for JScript. To view Visual Basic, C # or C examples, click the "Language Filter" button in the upper left corner.
Claim
Platform: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series, .NET Framework Lite
See
DataTable class | DataTable Member | System.Data Namespace | AcceptChanges | Add | DatacolumnCollection | Datacolumn | DataRowCollection