Updating the Dataset

From RAD Studio
Jump to: navigation, search

Go Up to Creating a Data Editing Control


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 Using the Windows API Messaging Solution.

To update the dataset within a message handler, follow these steps:

  1. Add the message handler to the TDBCalendar component:
type
  TDBCalendar = class(TSampleCalendar);
  private
    procedure CMExit(var Message: TWMNoParams); message CM_EXIT;
   .
   .
   .
  end; 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:

    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; 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:
type
  TDBCalendar = class(TSampleCalendar);
  private
    procedure DoExit; override;
   .
   .
   .
  end; class PACKAGE TDBCalendar : public TSampleCalendar
{
private:
    DYNAMIC void __fastcall DoExit(void);
   .
   .
   .
};
  1. Implement the DoExit method so it looks like this:

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

See Also