Data controls connect to datasets by using a data source. A data source component (TDataSource) acts as a conduit between the control and a dataset containing data. Each data-aware control must be associated with a data source component to have data to display and manipulate. Similarly, all datasets must be associated with a data source component in order for their data to be displayed and manipulated in data-aware controls on a form.
Note Data source components are also required for linking unnested datasets in masterdetail relationships.
To associate a data control with a dataset,
1 Place a dataset in a data module (or on a form), and set its properties as appropriate.
2 Place a data source in the same data module (or form). Using the Object Inspector, set its DataSet property to the dataset you placed in step 1.
3 Place a data control from the Data Access page of the Component palette onto a form.
4 Using the Object Inspector, set the DataSource property of the control to the data source component you placed in step 2.
5 Set the DataField property of the control to the name of a field to display, or select a field name from the drop-down list for the property. This step does not apply to TDBGrid, TDBCtrlGrid, and TDBNavigator because they access all available fields in the dataset.
6 Set the Active property of the dataset to True to display data in the control.
In the preceding example, the datasource was associated with its dataset by setting the DataSet property at design time. At runtime, you can switch the dataset for a data source component as needed. For example, the following code swaps the dataset for the CustSource data source component between the dataset components named Customers and Orders:
with CustSource do begin if (DataSet = Customers) then DataSet := Orders else
DataSet := Customers;
You can also set the DataSet property to a dataset on another form to synchronize the data controls on two forms. For example:
procedure TForm2.FormCreate (Sender : TObject); begin
DataSourcel.Dataset := Forml.Tablel; end;
The data source has an Enabled property that determines if it is connected to its dataset. When Enabled is True, the data source is connected to a dataset.
You can temporarily disconnect a single data source from its dataset by setting Enabled to False. When Enabled is False, all data controls attached to the data source component go blank and become inactive until Enabled is set to True. It is recommended, however, to control access to a dataset through a dataset component's DisableControls and EnableControls methods because they affect all attached data sources.
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 dataset's State property to determine its current state.
For example, the following OnStateChange event handler enables or disables buttons or menu items based on the current state:
Note For more information about dataset states, see "Determining dataset states" on page 18-3.
Was this article helpful?