Delphi - dynamically add all fields at runtime generate duplicates in dataset

Based on How to add a field programatically to a TAdoTable in Delphi I am trying to add all the fields dynamically to a FibPlus dataset (could be any TDataSet descendant). Each field is declared as a variable. Declarations part

  TForm4 = class(TForm)
    pFIBDatabase1: TpFIBDatabase;
    pFIBTransaction1: TpFIBTransaction;
    ds1: TpFIBDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
   iVERField : TFIBIntegerField;//I need that all fields to be represented by a variable
    { Public declarations }
  end;

and on the formcreate event, I create all the fields, set the needed properties and add dynamically all of them to the my dataset.

procedure TForm4.FormCreate(Sender: TObject);
var i:Integer;
    fieldDef :TFieldDef;
begin
 ds1.SQLs.SelectSQL.Text := 'select ver from ver';

 ds1.Fields.Clear;
 ds1.FieldDefs.Clear;
 ds1.FieldDefs.update;

 iVERField := TFIBIntegerField.Create(ds1);
 iVERField.FieldName := 'VER';
 iVERField.DisplayLabel := 'VER';
 iVERField.Name := 'iVERField';
 iVERField.DataSet := ds1;
 ds1.Fields.Add(iVERField);

 ds1.Open;
end;

My problem is that the field appear as a duplicate on a dbgrid

LE: Why field appears twice:

 iVERField.DataSet := ds1; //one 
 ds1.Fields.Add(iVERField);//two

LE1: Is this the way I should add all the fields as variables to the dataset?

Answers


In D7 (and I doubt it's changed since), TField's SetDataSet method already calls the Ffields.Add method of the dataset, so your explicit ds1.Fields.Add adds it a second time, hence the duplicated field.


Need Your Help

AngularJs Separating Results into Divs by column value

javascript angularjs logic resultset

I have been struggling to figure out the logic on how to display my results into separate divs in this case bootstrap col-md-4's. It is for a chat messaging app that i'm attempting to build. There is

Configuration of Static IP Address on Windows Server 2008 for ASP.NET application

asp.net configuration windows-server-2008-r2 static-ip-address

I have a system with Windows server 2008 installed on it. I have deployed a ASP.NET web application on this system after configuration of IIS. This application is running fine and users are able to