Adding a Row
As with untyped
DataSet objects, there are two ways to add a new
row to a strongly typed DataSet. The first uses
the
New
TableName
Row( ) method of the strongly typed DataSet
to return a reference to a
TableName
Row object.
The accessor properties are then used to assign values to the columns
of the new row. Finally, the
Add
TableName
Row( ) method adds the new row to the
DataTable. The following example demonstrates this
method:
// strongly typed DataSet called Northwind containing the Orders table Northwind.OrdersDataTable ordersTable = new Northwind.OrdersDataTable(); // create a new row object Northwind.OrdersRow ordersRow = ordersTable.NewOrdersRow(); // use property accessors to set the column values for the row ordersRow.CustomerID = "VINET"; ordersRow.EmployeeID = 1; // ... set the rest of the fields // add the row to the table ordersTable.AddOrdersRow(ordersRow);
The following code sample shows how the same thing can be
accomplished with an untyped DataSet:
DataTable ordersTable = new DataTable("Orders");
// ... code to define or retrieve the schema for the DataTable
DataRow ordersRow = ordersTable.NewRow();
ordersRow["CustomerID"] = "VINET";
ordersRow["EmployeeID"] = 1;
// ... set the rest of the fields
ordersTable.Rows.Add(ordersRow);The second way to add a new row to a strongly typed
DataSet is to use the
Add
TableName
Row( ) method. This method allows a row to be added to the
DataSet using a single statement similar to the
Add( ) method of the
DataRowCollection ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access