RAD Studio |  |

Updating the Field Data Link Class

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:

Copy Code

    type
      TDBCalendar = class(TSampleCalendar);
      private
        procedure UpdateData(Sender: TObject);
       .
       .
       .
      end;

Copy Code

    class PACKAGE TDBCalendar : public TSampleCalendar
    {
    private:
        void __fastcall UpdateData(TObject *Sender);
    };

1. Implement the UpdateData method:

Copy Code

    procedure UpdateData(Sender: TObject);
    begin
      FDataLink.Field.AsDateTime := CalendarDate;        { set field link to calendar date }
    end;

Copy Code

    void __fastcall TDBCalendar::UpdateData( TObject* Sender)
    {
        FDataLink->Field->AsDateTime = CalendarDate;     // set field link to calendar date
    }

1. Within the constructor for TDBCalendar, assign the UpdateData method to the OnUpdateData event:

Copy Code

    constructor TDBCalendar.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FReadOnly := True;
      FDataLink := TFieldDataLink.Create;
      FDataLink.OnDataChange := DataChange;
      FDataLink.OnUpdateData := UpdateData;
    end;

Copy Code

    __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
    }

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!