Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculated Field created in runtime

1,170 views
Skip to first unread message

Diego Osorio

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to
I'm trying to attach a new calculated field to a TClientDataSet during
runtime. Does anyone have an idea on how to do this ??

Brian Bushay TeamB

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to

>'m trying to attach a new calculated field to a TClientDataSet during
>runtime. Does anyone have an idea on how to do this ??

here is an example
2) In the FormCreate, create your calculated field:
(For this example,use DBDEMOS/EMPLOYEE.DB)

Table1.Active:=false; // Must be false to add Fields to the Dataset
Table1.FieldDefs.Update; // Make sure we have the latest Fields

Monthly:=TFloatField.Create(Table1);
Monthly.FieldName:='Monthly Salary';
Monthly.Calculated:=true;
Monthly.Dataset:=Table1;
Monthly.DisplayFormat:='$#,0.00';

Table1.Active:=true;

--
Brian Bushay (TeamB)
Bbu...@DataGuidance.com

Roman Krejci

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
Hi Brian,
this will work - but the prerecquisite is that
the table has persistentl physical fields.
if it has not, after the final open the table will have
FieldCount = 1 and will contain only
'Monthly Salary' field. If OnClacFields event refers
to any other fields, an exception will be thrown.
--
Roman
(please remove STOPSPAM. in header)
URL: www.rksolution.cz (Delphi corner)
MAIL: IN...@rksolution.cz

Brian Bushay TeamB wrote in message
<35f3bc66...@forums.borland.com>...

Diego Osorio

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
Yes, Roman is right. I tried the code against my ClientDataSet (which is
created in memory only at runtime) and the end result is that there is only
one field in the DataSet.

I use a local client data set to contain the data provided by a SQL query.

ClientDataSet1 := TClientDataSet.Create( Application );
ClientDataSet1.Provider := sqlQry.Provider;
ClientDataSet1.OnCalcFields := SomeOnCalcFields;

sqlQry.Open;
ClientDataSet1.Open;

Is there any way to add a field to a dataset created this way or is there
any other approach that you recommend.

Roman Krejci

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
The only way is to have persistent fields.
If you cannot add persistent fields at design time,
create them at runtime .

Brian Bushay TeamB

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to

>
>Is there any way to add a field to a dataset created this way or is there
>any other approach that you recommend.

You have to have persistant fields to add a calculated field. You can create
the pesistant fields in code too.
Here is another example

var
f : TField;
i : integer;
begin
Table1.Close;
//itterate through fields and create persistant fields
for i := 0 to Table1.FieldDefs.Count - 1 do
Table1.FieldDefs.Items[i].CreateField(Table1);
//create new calculated field
f := TStringField.Create(Table1);
f.Name := 'Table1CalcField';
f.FieldName := 'CalcField';
f.DisplayLabel := 'CalcField';
f.Calculated := True;
f.DataSet := Table1;
//open the table for use
Table1.Open;
end;

Diego Osorio

unread,
Sep 8, 1998, 3:00:00 AM9/8/98
to
Thank you very much. It works just the way I want it.

Brian Bushay TeamB wrote in message

<35f0f0b1...@forums.borland.com>...

0 new messages