RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TSimpleDataSet.FieldDefs Property

Points to the list of field definitions for the dataset.

Pascal
property FieldDefs: TFieldDefs;
C++
__property TFieldDefs FieldDefs;

FieldDefs lists the field definitions for a dataset. While an application can examine FieldDefs to explore the field definitions for a dataset, it should not change these definitions unless creating a new table with CreateTable or CreateDataSet. 

To access fields and field values in a dataset, use the Fields, AggFields, and FieldValues properties, and the FieldsByName method.

Note: If the dataset includes object field descendants, FieldDefs represents a hierarchical view of the data, meaning that the definitions include object field definitions. To determine the definitions in a flattened view, use FieldDefList instead.
 

C++ Examples: 

 

/*
The following code creates and activates a client dataset in
the form’s OnCreate event handler.
*/ 
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TFieldDefs *pDefs = CDS2->FieldDefs;
  TFieldDef *pDef = pDefs->AddFieldDef();
  pDef->DataType = ftInteger;
  pDef->Name = "Field1";

  pDef = pDefs->AddFieldDef();
  pDef->DataType = ftString;
  pDef->Size = 10;
  pDef->Name = "Field2";

  TIndexDef *pIDef = CDS2->IndexDefs->AddIndexDef();
  pIDef->Fields = "Field1";
  pIDef->Name = "IntIndex";

  CDS2->CreateDataSet();
}
/*
The following example shows how to create a table.
*/

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

 

Delphi Examples: 

{
The following code creates and activates a client dataset in
the form’s OnCreate event handler.
} 
procedure TForm1.FormCreate(Sender: TObject);
begin
  with CDS2 do
  begin
    with FieldDefs.AddFieldDef do
    begin
      DataType := ftInteger;
      Name := 'Field1';
    end;
    with FieldDefs.AddFieldDef do
    begin
      DataType := ftString;
      Size := 10;
      Name := 'Field2';
    end;
    with IndexDefs.AddIndexDef do
    begin
      Fields := 'Field1';
      Name := 'IntIndex';
    end;
    CreateDataSet;
  end;
end; 

 

{ The following example shows how to create a table. }
procedure TForm1.Button1Click(Sender: TObject);
begin
  Table1.Close;
  Table1.DeleteTable;
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:= True;
end;

 

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