Icon Responding to DataSet Changes

It is important that one be able to respond to various changes to dataset columns and rows, both for updating control states and for implementing concepts such as master-detail linkages. The TDataSet OnStateChange event is used to track when the TDataSet State changes. The TDataSet OnRowChanged event is used to track when the active row in the dataset changes, or when a column in the active row changes.

State Changes
Define an event handler for the TDataSet OnStateChange event in order to track when the dataset state changes. The following list details the TDataSet methods that cause the dataset state to change, along with the state after the method completes:

MethodAfter State
OpendsBrowse
ClosedsClosed
CheckBrowseModedsBrowse (if result is True)
InitFinddsFind
FinddsBrowse
InsertdsInsert
UpdatedsUpdate
SavedsBrowse
CanceldsBrowse

The following example shows an OnStateChange event handler that displays the state of a dataset called "Vendors" in a label on the current form:

procedure TMyForm.VendorsStateChange(Sender: TObject);
begin
   case Vendors.State of
      dsClosed:
         VendorStateLabel.Caption:='Closed';
      dsBrowse:
         VendorStateLabel.Caption:='Browse';
      dsInsert:
         VendorStateLabel.Caption:='Insert';
      dsUpdate:
         VendorStateLabel.Caption:='Update';
      dsFind:
         VendorStateLabel.Caption:='Find';
      end;
end;

Row Changes
Define an event handler for the TDataSet OnRowChanged event in order to track when the active row in the dataset changes. The OnRowChanged event is fired when any column in the active row is changed due to a modification, or when the active row changes due to the row pointer moving. If the OnRowChanged event was fired in response to a column modification, then the Column parameter in the event handler will contain an instance of the TDataColumn that was modified. If the OnRowChanged event was fired in response to the entire active row changing, then the Column parameter will be nil.

The following TDataColumn properties and methods will cause the OnRowChanged event to fire with a non-nil Column parameter:

   AsString
   AsBoolean
   AsInteger
   AsFloat
   AsDate
   AsTime
   AsDateTime
   Clear

The following TDataSet properties and methods will cause the OnRowChanged event to fire with a nil Column parameter:

   RowID
   LoadRows
   Sort
   EnableControls
   First
   Prior
   Next
   Last
   MoveBy
   MoveTo
   GotoBookmark
   InitFind
   Find
   Insert
   Save
   Cancel
   Delete

The following TDatabase properties and methods will cause the OnRowChanged event to fire with a nil Column parameter:

   LoadRows
   Rollback

Responding to row changes is important for updating related controls in the user interface. The following example shows an OnRowChanged event handler that responds to row changes by calling methods that update both buttons and labels:

procedure TMasterDetailForm.CustomerOrdersRowChanged(Sender: TObject;
                                                     Column: TDataColumn);
begin
   if (Column=nil) then
      begin
      UpdateOrderButtons;
      UpdateOrderLabels;
      end;
end;

Responding to row changes is also important for concepts such as master-detail links. The following example shows an OnRowChanged event handler that responds to row changes for loading a detail dataset as the master row changes:

procedure TMasterDetailForm.LoadOrders;
begin
   CustomerOrders.Params.Clear;
   CustomerOrders.Params.Add('CustomerID='''+
                             Customer.Columns['CustomerID'].AsString+
                             '''');
   Database.LoadRows(CustomerOrders);
end;

procedure TMasterDetailForm.CustomerRowChanged(Sender: TObject;
                                               Column: TDataColumn);
begin
   if (Column=nil) then
      begin
      UpdateCustomerButtons;
      LoadOrders;
      end;
end;
Image