Go Up to Understanding datasets Index
A dataset must be in dsEdit mode before an application can modify records. In your code you can use the Edit method to put a dataset into dsEdit mode if the read-only CanModify property for the dataset is True.
When a dataset transitions to dsEdit mode, it first receives a BeforeEdit event. After the transition to edit mode is successfully completed, the dataset receives an AfterEdit event. Typically, these events are used for updating the user interface to indicate the current state of the dataset. If the dataset can't be put into edit mode for some reason, an OnEditError event occurs, where you can inform the user of the problem or try to correct the situation that prevented the dataset from entering edit mode.
On forms in your application, some data-aware controls can automatically put a dataset into dsEdit state if
- The control's ReadOnly property is False (the default),
- The AutoEdit property of the data source for the control is True, and
- CanModify is True for the dataset.
Note: Even if a dataset is in dsEdit state, editing records may not succeed for SQL-based databases if your application's user does not have proper SQL access privileges.
Once a dataset is in dsEdit mode, a user can modify the field values for the current record that appears in any data-aware controls on a form. Data-aware controls for which editing is enabled automatically call Post when a user executes any action that changes the current record (such as moving to a different record in a grid).
If you have a navigator component on your form, users can cancel edits by clicking the navigator's Cancel button. Canceling edits returns a dataset to dsBrowse state.
In code, you must write or cancel edits by calling the appropriate methods. You write changes by calling Post. You cancel them by calling Cancel. In code, Edit and Post are often used together. For example,
with CustTable do begin Edit; FieldValues['CustNo'] := 1234; Post; end;
Table1->Edit(); Table1->FieldValues["CustNo"] = 1234; Table1->Post();
In the previous example, the first line of code places the dataset in dsEdit mode. The next line of code assigns the number 1234 to the CustNo field of the current record. Finally, the last line writes, or posts, the modified record. If you are not caching updates, posting writes the change back to the database. If you are caching updates, the change is written to a temporary buffer, where it stays until the dataset's ApplyUpdates method is called.
- Adding New Records
- Deleting Records
- Posting Data to the Database
- Canceling Changes
- Modifying Entire Records