DataTable.NewRow Method
Creates a new DataRow with the same schema as the table.
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. 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; }
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.
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.
- 1/29/2012
- Engr Faraz Khan
- 5/25/2011
- K Z
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..
- 3/24/2011
- Marshpa