RAD Studio
ContentsIndex
PreviousUpNext
Responding to Changes Mediated by the Data Source

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 UpdateData 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 StateChange 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:

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;

 

void __fastcall TForm1::DataSource1StateChange(TObject *Sender)
{
  CustTableActivateBtn->Enabled = (CustTable->State == dsInactive);
  CustTableEditBtn->Enabled = (CustTable->State == dsBrowse);
  CustTableCancelBtn->Enabled = (CustTable->State == dsInsert ||
                                 CustTable->State == dsEdit ||
                                 CustTable->State == dsSetKey);
  .
  .
  .
}

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

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!