Share via


Modifying DataViews (ADO.NET)

You can use the DataView to add, delete, or modify rows of data in the underlying table. The ability to use the DataView to modify data in the underlying table is controlled by setting one of three Boolean properties of the DataView. These properties are AllowNew, AllowEdit, and AllowDelete. They are set to true by default.

If AllowNew is true, you can use the AddNew method of the DataView to create a new DataRowView. Note that a new row is not actually added to the underlying DataTable until the EndEdit method of the DataRowView is called. If the CancelEdit method of the DataRowView is called, the new row is discarded. Note also that you can edit only one DataRowView at a time. If you call the AddNew or BeginEdit method of the DataRowView while a pending row exists, EndEdit is implicitly called on the pending row. When EndEdit is called, the changes are applied to the underlying DataTable and can later be committed or rejected using the AcceptChanges or RejectChanges methods of the DataTable, DataSet, or DataRow object. If AllowNew is false, an exception is thrown if you call the AddNew method of the DataRowView.

If AllowEdit is true, you can modify the contents of a DataRow via the DataRowView. You can confirm changes to the underlying row using DataRowView.EndEdit or reject the changes using DataRowView.CancelEdit. Note that only one row can be edited at a time. If you call the AddNew or BeginEdit methods of the DataRowView while a pending row exists, EndEdit is implicitly called on the pending row. When EndEdit is called, proposed changes are placed in the Current row version of the underlying DataRow and can later be committed or rejected using the AcceptChanges or RejectChanges methods of the DataTable, DataSet, or DataRow object. If AllowEdit is false, an exception is thrown if you attempt to modify a value in the DataView.

When an existing DataRowView is being edited, events of the underlying DataTable will still be raised with the proposed changes. Note that if you call EndEdit or CancelEdit on the underlying DataRow, pending changes will be applied or canceled regardless of whether EndEdit or CancelEdit is called on the DataRowView.

If AllowDelete is true, you can delete rows from the DataView by using the Delete method of the DataView or DataRowView object, and the rows are deleted from the underlying DataTable. You can later commit or reject the deletes using AcceptChanges or RejectChanges respectively. If AllowDelete is false, an exception is thrown if you call the Delete method of the DataView or DataRowView.

The following code example disables using the DataView to delete rows and adds a new row to the underlying table using the DataView.

Dim custTable As DataTable = custDS.Tables("Customers")
Dim custView As DataView = custTable.DefaultView
custView.Sort = "CompanyName"

custView.AllowDelete = False

Dim newDRV As DataRowView = custView.AddNew()
newDRV("CustomerID") = "ABCDE"
newDRV("CompanyName") = "ABC Products"
newDRV.EndEdit()
DataTable custTable = custDS.Tables["Customers"];
DataView custView = custTable.DefaultView;
custView.Sort = "CompanyName";

custView.AllowDelete = false;

DataRowView newDRV = custView.AddNew();
newDRV["CustomerID"] = "ABCDE";
newDRV["CompanyName"] = "ABC Products";
newDRV.EndEdit();

See Also

Reference

DataTable

DataView

DataRowView

Other Resources

DataViews (ADO.NET)

ADO.NET Managed Providers and DataSet Developer Center