RAD Studio |  |

Updating the Dataset

So far, a change within the data-aware control has changed values in the field data link class. The final step in creating a data editing control is to update the dataset with the new value. This should happen after the person changing the value in the data-aware control exits the control by clicking outside the control or pressing the Tab key.

Note: VCL applications define message control IDs for operations on controls. For example, the CM_EXIT message is sent to the control when the user exits the control. You can write message handlers that respond to the message. In this case, when the user exits the control, the CMExit method, the message handler for CM_EXIT, responds by updating the record in the dataset with the changed values in the field data link class. For more information about message handlers, see Handling messages and system notifications.
To update the dataset within a message handler, follow these steps:

1. Add the message handler to the TDBCalendar component:

Copy Code

    type
      TDBCalendar = class(TSampleCalendar);
      private
        procedure CMExit(var Message: TWMNoParams); message CM_EXIT;
       .
       .
       .
      end;

Copy Code

    class PACKAGE TDBCalendar : public TSampleCalendar
    {
    private:
        void __fastcall CMExit(TWMNoParams Message);
    BEGIN_MESSAGE_MAP
      MESSAGE_HANDLER(CM_EXIT, TWMNoParams, CMExit)
    END_MESSAGE_MAP
    };

1. Implement the CMExit method so it looks like this:

Copy Code

    procedure TDBCalendar.CMExit(var Message: TWMNoParams);
    begin
      try
        FDataLink.UpdateRecord;                          { tell data link to update database }
      except
        on Exception do SetFocus;                      { if it failed, don't let focus leave }
     end;
      inherited;
    end;

Copy Code

    void __fastcall TDBCalendar::CMExit(TWMNoParams &Message)
    {
      try
      {
        FDataLink.UpdateRecord();          // tell data link to update database
      }
      catch(...)
      {
        SetFocus();                        // if it failed, don't let focus leave
        throw;
      }
    }

To update the dataset when the user exits the control, follow these steps:

1. Add an override for the DoExit method to the TDBCalendar component:

Copy Code

    type
      TDBCalendar = class(TSampleCalendar);
      private
        procedure DoExit; override;
       .
       .
       .
      end;

Copy Code

    class PACKAGE TDBCalendar : public TSampleCalendar
    {
    private:
        DYNAMIC void __fastcall DoExit(void);
       .
       .
       .
    };

1. Implement the DoExit method so it looks like this:

Copy Code

    procedure TDBCalendar.CMExit(var Message: TWMNoParams);
    begin
      try
        FDataLink.UpdateRecord;                          { tell data link to update database }
      except
        on Exception do SetFocus;                      { if it failed, don't let focus leave }
     end;
      inherited;                         { let the inherited method generate an OnExit event }
    end;

Copy Code

    void __fastcall TDBCalendar::DoExit(void)
    {
      try
      {
        FDataLink.UpdateRecord();          // tell data link to update database
      }
      catch(...)
      {
        SetFocus();                        // if it failed, don't let focus leave
        throw;
      }
      TCustomGrid::DoExit(); // let the inherited method generate an OnExit event
    }

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