RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TDataSet.Refresh Method

Re-fetches data from the database to update a dataset's view of data.

Pascal
procedure Refresh;
C++
__fastcall Refresh();

Call Refresh to ensure that an application has the latest data from a database. For example, when an application turns off filtering for a dataset, it should immediately call Refresh to display all records in the dataset, not just those that used to meet the filter condition.

Note: The Refresh method does not work for all TDataSet descendants. In particular, TQuery components do not support the Refresh method if the query is not "live". To refresh a static TQuery, close and reopen the dataset.
TDataSet generates a BeforeRefresh event before refreshing the records and an AfterRefresh event afterwards.
Note: Most datasets try to maintain the current record position when you call refresh. However, this is not always possible. For example, the current record may have been deleted from the server by another user. Unidirectional datasets have no mechanism for locating the current record after a refresh, and always move back to the first record.
Warning: Unidirectional datasets refresh the data by closing and reopening the cursor. This can have unintended side effects if, for example, you have code in the BeforeClose, AfterClose, BeforeOpen, or AfterOpen event handlers.
 

Delphi Examples: 

 

{
The following example sets a range for a dataset. The form
requires two edit boxes, a data source, a client dataset or
table, a db grid, and a button.  Make sure that the Table1
info in FormActivate agrees with the table used.  Also, the
caption for the button must be initialized to '&ApplyRange'.
}

procedure TForm1.FormActivate(Sender: TObject);
begin
  Table1.DatabaseName := 'DBDEMOS';
  Table1.TableName := 'CustInfo';
  Table1.Active := True;
  Table1.IndexName := '';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if Button2.Caption = '&Apply Range' then
    begin
      Table1.SetRange([Edit1.Text],[Edit2.Text]);
      Button2.Caption := '&Drop Range';
    end
  else
    begin
      Table1.CancelRange;
      Table1.Refresh;
      Button2.Caption := '&Apply Range';
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Table1:= TTable.Create(Form1);
  with Table1 do
  begin
    DatabaseName := 'DBDEMOS';
    TableType := ttParadox;
    TableName := 'CustInfo';
    Table1.Active := False;
    { Don't overwrite an existing table }
    if Table1.Exists then
      MessageDlg('CustInfo table already exists.', mtWarning, [mbOK], 0)
    else
    begin
      { The Table component must not be active }
      { First, describe the type of table and give }
      { it a name }
      { Next, describe the fields in the table }
      with FieldDefs do
      begin
        Clear;
        with AddFieldDef do
        begin
          Name := 'Field1';
          DataType := ftInteger;
          Required := True;
        end;
        with AddFieldDef do
        begin
          Name := 'Field2';
          DataType := ftString;
          Size := 30;
        end;
      end;
      { Next, describe any indexes }
      with IndexDefs do
      begin
        Clear;
        { The 1st index has no name because it is
        { a Paradox primary key }
        with AddIndexDef do
        begin
          Name := '';
          Fields := 'Field1';
          Options := [ixPrimary];
        end;
        with AddIndexDef do
        begin
          Name := 'Fld2Indx';
          Fields := 'Field2';
          Options := [ixCaseInsensitive];
        end;
      end;
      { Call the CreateTable method to create the table }
      CreateTable;
      Table1.Active:= True;
      for i := 1 to 20 do
        Table1.AppendRecord([i*111, i*222]);
    end;
  end;
  DS2.DataSet:= Table1;
  DBGrid2.DataSource.DataSet:= Table1;
  Table1.Active:= False;
end;

 

CheckBrowseMode 

Resync 

UpdateCursorPos 

BeforeRefresh 

AfterRefresh 

Refreshing Data Display 

Refreshing Records

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!