Responding to Changes Mediated by the Data Source

From RAD Studio
Jump to: navigation, search

Go Up to Associating a Data Control with a Dataset


Because the data source provides the link between the data control and its dataset, it mediates all of the communication that occurs between the two. Typically, the data-aware control automatically responds to changes in the dataset. However, if your user interface is using controls that are not data-aware, you can use the events of a data source component to manually provide the same sort of response.

The OnDataChange event occurs whenever the data in a record may have changed, including field edits or when the cursor moves to a new record. This event is useful for making sure the control reflects the current field values in the dataset, because it is triggered by all changes. Typically, an OnDataChange event handler refreshes the value of a non-data-aware control that displays field data.

The OnUpdateData event occurs when the data in the current record is about to be posted. For instance, an OnUpdateData event occurs after Post is called, but before the data is actually posted to the underlying database server or local cache.

The OnStateChange event occurs when the state of the dataset changes. When this event occurs, you can examine the State property of the dataset to determine its current state.

For example, the following OnStateChange event handler enables or disables buttons or menu items based on the current state:

 procedure Form1.DataSource1.StateChange(Sender: TObject);
 begin
   CustTableEditBtn.Enabled := (CustTable.State = dsBrowse);
   CustTableCancelBtn.Enabled := CustTable.State in [dsInsert, dsEdit, dsSetKey];
   CustTableActivateBtn.Enabled := CustTable.State in [dsInactive];
   { ... }
 end;

Note: For more information about dataset states, see Determining Dataset States.

See Also