How to: Add Rows to a DataTable
To add new records into a dataset, a new data row must be created and added to the DataRow collection (Rows) of a DataTable in the dataset. The following procedures show how to create a new row and insert it into a DataTable. Examples are provided for both typed and untyped datasets.
Applications that use data-bound controls typically get the ability to add new records through the "add new" button on a BindingNavigator Control.
For this example, it is assumed that a dataset has a Customers DataTable and has two columns named CustomerID and CompanyName. Typed datasets expose the column names as properties of the typed DataRow object; in this case the CustomersRow.
To add a new record to a typed dataset
Declare a new instance of the typed dataset. In the following example, you declare a new instance of the CustomersRow class, assign it a new row, populate the columns with data, and add the new row to the Customers table's Rows collection:
For this example, it is assumed that the untyped dataset has a Customers DataTable that has two columns named CustomerID and CompanyName. Untyped datasets require knowledge of column names or indices when coding. This example uses column names.