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 ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.