Edit data in datasets
You edit data in data tables much like you edit the data in a table in any database. The process can include inserting, updating, and deleting records in the table. In a data-bound form, you can specify which fields are user-editable. In those cases, the data-binding infrastructure handles all the change tracking so that the changes can be sent back to the database later. If you programmatically make edits to data, and you intend to send those changes back to the database, you must use the objects and methods that do the change tracking for you.
In addition to changing the actual data, you can also query a DataTable to return specific rows of data. For example, you might query for individual rows, specific versions of rows (original and proposed), rows that have changed, or rows that have errors.
If you don't know the index of the row you want to edit, use the
FindBy method to search by the primary key:
NorthwindDataSet.CustomersRow customersRow = northwindDataSet1.Customers.FindByCustomerID("ALFKI"); customersRow.CompanyName = "Updated Company Name"; customersRow.City = "Seattle";;
If you know the row index, you can access and edits rows as follows:
northwindDataSet1.Customers.CompanyName = "Updated Company Name"; northwindDataSet1.Customers.City = "Seattle";
Applications that use data-bound controls typically add new records through the Add New button on a BindingNavigator Control.
NorthwindDataSet.CustomersRow newCustomersRow = northwindDataSet1.Customers.NewCustomersRow(); newCustomersRow.CustomerID = "ALFKI"; newCustomersRow.CompanyName = "Alfreds Futterkiste"; northwindDataSet1.Customers.Rows.Add(newCustomersRow);
In order to retain the information that the dataset needs to send updates to the data source, use the Delete method to remove rows in a data table. For example, if your application uses a TableAdapter (or DataAdapter), the TableAdapter's
Update method deletes rows in the database that have a RowState of Deleted.
If your application does not need to send updates back to a data source, then it's possible to remove records by directly accessing the data row collection (Remove).
This method doesn't physically remove the record. Instead, it marks the record for deletion.
If you get the count property of a DataRowCollection, the resulting count includes records that have been marked for deletion. To get an accurate count of records that aren't marked for deletion, you can loop through the collection looking at the RowState property of each record. (Records marked for deletion have a RowState of Deleted.) Alternatively, you can create a data view of a dataset that filters based on row state and get the count property from there.
The following example shows how to call the Delete method to mark the first row in the
Customerstable as deleted:
When changes are made to records in a dataset, information about those changes is stored until you commit them. You commit the changes when you call the
AcceptChanges method of a dataset or data table, or when you call the
Update method of a TableAdapter or data adapter.
Changes are tracked two ways in each data row:
Each changed data row contains multiple versions of that row (DataRowVersion), the original version (before changes), and the current version (after changes). During the period when a change is pending (the time when you can respond to the RowChanging event), a third version — the proposed version— is available as well. For more information, see How to: Get Specific Versions of a DataRow.
The HasChanges method of a dataset returns
true if changes have been made in the dataset. After determining that changed rows exist, you can call the
GetChanges method of a DataSet or DataTable to return a set of changed rows. For more information, see How to: Retrieve Changed Rows.
Call the HasChanges method of a dataset to check for changed rows.
The following example shows how to check the return value from the HasChanges method to detect whether there are any changed rows in a dataset named
The following example shows how to check a dataset named
NorthwindDataset1to determine if any new rows have been added to it:
HasErrorsproperty to see if there are any errors in the dataset.
true, iterate through the collections of tables, and then the through the rows, to find the row with the error.