Updating the Field Data Link Class

From RAD Studio
Jump to: navigation, search

Go Up to Creating a Data Editing Control


There are two types of data changes:

  • A change in a field value that must be reflected in the data-aware control.
  • A change in the data-aware control that must be reflected in the field value.

The TDBCalendar component already has a DataChange method that handles a change in the field's value in the dataset by assigning that value to the CalendarDate property. The DataChange method is the handler for the OnDataChange event. So the calendar component can handle the first type of data change.

Similarly, the field data link class also has an OnUpdateData event that occurs as the user of the control modifies the contents of the data-aware control. The calendar control has a UpdateData method that becomes the event handler for the OnUpdateData event. UpdateData assigns the changed value in the data-aware control to the field data link.

To reflect a change made to the value in the calendar in the field value:

  1. Add an UpdateData method to the private section of the calendar component:
type
  TDBCalendar = class(TSampleCalendar);
  private
    procedure UpdateData(Sender: TObject);
   .
   .
   .
  end; 
class PACKAGE TDBCalendar : public TSampleCalendar
{
private:
    void __fastcall UpdateData(TObject *Sender);
};
  1. Implement the UpdateData method:

    procedure UpdateData(Sender: TObject);
    begin
      FDataLink.Field.AsDateTime := CalendarDate;        { set field link to calendar date }
    end; void __fastcall TDBCalendar::UpdateData( TObject* Sender)
    {
        FDataLink->Field->AsDateTime = CalendarDate;     // set field link to calendar date
    }
    
  2. Within the constructor for TDBCalendar, assign the UpdateData method to the OnUpdateData event:

    constructor TDBCalendar.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FReadOnly := True;
      FDataLink := TFieldDataLink.Create;
      FDataLink.OnDataChange := DataChange;
      FDataLink.OnUpdateData := UpdateData;
    end; 
    
    __fastcall TDBCalendar::TDBCalendar(TComponent* Owner)
      : TSampleCalendar(Owner)
    {
      FDataLink = new TFieldDataLink();       // this was already here
      FDataLink->OnDataChange = DataChange;   // this was here too
      FDataLink->OnUpdateData = UpdateData;   // assign UpdateData to the OnUpdateData event
    }
    

See Also