-->

Cannot add calculation field to clientdataset

2019-07-29 05:44发布

问题:

I am trying to add a calculation field to an existing data set created with fieldDefs.add using the code below with many variations I found on several sites including stack overflow.

But the fieldDefs created by the fieldDefs line or by the dataSet line are lost on update by FieldDefs.count.

Fields.counts persist but are not saved to XML files.

No exceptions are raised.

Does anyone have any idea what is going on here? Thanks

MYclientDataSet.CreateDataSet;
MYclientDataSet.open;
MYclientDataSet.FieldDefs.Update;
MYclientDataSet.Active := False;
for i := 0 to MYclientDataSet.FieldDefs.Count - 1 do
    MYclientDataSet.FieldDefs[i].CreateField(MYclientDataSet);
fld := TStringField.Create(MYclientDataSet);
with fld do begin
     FieldName := 'PartSummary';
     FieldKind := fkCalculated;
     Calculated := True;
     Name := cds.Name + FieldName;
     DataSet := MYclientDataSet;
     MYclientDataSet.FieldDefs.Add('PartSummary', ftString, 30, false);
     MYclientDataSet.FieldDefs.update;
end;
MYclientDataSet.active := true;
MYclientDataSet.open;
MYclientDataSet.edit;

回答1:

According to Cary Jensen in the Delphi education link Defining a Clientdataset's Structure... you cannot create a calculated, lookup, aggregate field using FieldDefs. You must use TFields instead.