{> --------------------------------------------------------------------------------------------- ----------------
Process function: add calculation fields to data
Modify modification:
Increase parameters: name: String
Xtype: Integer is used to specify field type
0: for Integer 1: for String
Role: To specify the field name to add the calculation field, it is conducive to the scope of the extension process
<----------------------------------------------- ------------}
Procedure addCalcfield (CDS: TclientDataSet; Aname: string; xtype: integer);
VAR
FieldList: TLIST;
FieldItem: TfieldInfo;
FDATA: Olevariant;
I: integer;
Begin
IF not cds.active kil
EXIT;
FDATA: = CDS.DATA;
Fieldlist: = tlist.create;
For i: = 0 to cds.fielddefs.count-1 do
Begin
NEW (FieldItem);
FieldItem ^ .fieldclass: = cds.fielddefs.Items [i] .fieldclass;
FieldItem ^ .fieldName: = cds.fielddefs.Items [i] .name;
FieldItem ^ .size: = cds.fielddefs.Items [i] .size;
FieldItem ^ .index: = cds.fielddefs.Items [i] .index;
FieldList.Add (FieldItem);
END;
If cds.findfield (aname) <> nil kil dam // If you have created no longer create 'gridxh'
CDS.Close;
Cds.fielddefs.beginupdate;
{> ----------- Create a calculation field ----------------}
Case Xtype of
0:
With Tintegerfield.create (CDS) DO
Begin
FieldName: = aname;
DisplayLabel: = aname;
Fieldkind: = fkcalculate;
Visible: = true;
DataSet: = CDS;
END;
1:
With TStringfield.create (CDS) DO
Begin
FieldName: = aname;
DisplayLabel: = aname;
Fieldkind: = fkcalculate;
Visible: = true;
DataSet: = CDS;
END;
End; // case
{<-------------------------------------}
Cds.fielddefs.endupdate;
For i: = 0 to FieldList.count-1 DO
Begin
If cds.findfield (tfieldinfo (fieldList.Items [i]) ^. FieldName) = nil dam
With TfieldInfo (FieldList.Items [i]) ^. Fieldclass.create (CDS) DO
Begin
FieldName: = TfieldInfo (FieldList.Items [i]) ^. FieldName;
Size: = TfieldInfo (FieldList.Items [i]) ^. size;
Index: = tfieldInfo (FieldList.Items [i]) ^. Index; DataSet: = CDS;
Freemem (FieldList.Items [I], SIZEOF (TfieldInfo);
END;
END;
FieldList.Free;
CDS.AppendData (FDATA, FALSE);