RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TDataSet.First Method

Moves to the first record in the dataset.

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

Call First to make the first record in the dataset active. First posts any changes to the active record and: 

Clears the record buffers. 

Fetches the first record and makes it the active record. 

Fetches any additional records required for display, such as those needed to fill out a grid control. 

Sets the Bof property to true. 

Broadcasts the record change so that data controls and linked detail sets can update.

Note: TDataSet uses internal, protected methods to reposition the active record and to fetch additional records required for display. In TDataSet, these internal methods are empty stubs. Descendant classes implement these methods to enable the First method to work.
 

C++ Examples: 

 

/*
Reads through all records in the Customers table.
Updates the ProgressBar accordingly.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ProgressBar1->Min = 0;
  ProgressBar1->Max = Customers->RecordCount;
  Customers->First();
  for (int i = ProgressBar1->Min; i <= ProgressBar1->Max; i++)
  {
    ProgressBar1->Position = i;
    Customers->Next();
    // do something with the current record
  }
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Customers = new TTable(Form1);
  Customers->Active = false; // The Table component must not be active
  Customers->DatabaseName = "DBDEMOS";
  Customers->TableType = ttParadox;
  Customers->TableName = "CustInfo";
  Customers->Active = False;
  if (Customers->Exists) // Don't overwrite an existing table
  {
    Customers->Close();
    Customers->DeleteTable();
  }
  // 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
  Customers->FieldDefs->Clear();
  TFieldDef *pNewDef = Customers->FieldDefs->AddFieldDef();
  pNewDef->Name = "Field1";
  pNewDef->DataType = ftInteger;
  pNewDef->Required = true;
  pNewDef = Customers->FieldDefs->AddFieldDef();
  pNewDef->Name = "Field2";
  pNewDef->DataType = ftString;
  pNewDef->Size = 30;
  // Next, describe any indexes
  Customers->IndexDefs->Clear();
  /* the 1st index has no name because it is a Paradox primary key */
  Customers->IndexDefs->Add("","Field1", TIndexOptions() <<ixPrimary << ixUnique);
  Customers->IndexDefs->Add("Fld2Index","Field2", TIndexOptions() << ixCaseInsensitive);
  // Now that we have specified what we want, create the table
  Customers->CreateTable();
  Customers->Active = True;
  for (int i = 1; i <= 20; i++)
    Customers->AppendRecord(ARRAYOFCONST((i*111, i*222)));
  DS2->DataSet = Customers;
  DBGrid2->DataSource->DataSet = Customers;
  Customers->Active = True;
}

 

Delphi Examples: 

{
Reads through all records in the Customers table.
Updates the ProgressBar accordingly.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  with ProgressBar1 do
  begin
    Min := 0;
    Max := Customers.RecordCount;
    Customers.First;
    for i := Min to Max do
    begin
      Position := i;
      Customers.Next;
      // do something with the current record
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Customers:= TTable.Create(Form1);
  with Customers do
  begin
    DatabaseName := 'DBDEMOS';
    TableType := ttParadox;
    TableName := 'CustInfo';
    Active := False;
//  Overwrite any existing table
    if Customers.Exists then
    begin
      Customers.Close;
      Customers.DeleteTable;
    end;
    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;
      Customers.Active:= True;
      for i := 1 to 100 do
        Customers.AppendRecord([i*111, i*222]);
    end;
  end;
  DS2.DataSet:= Customers;
  DBGrid2.DataSource.DataSet:= Customers;
  Customers.Active:= True;
end;

 

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