Solved

Add a calculated field to a TQuery at runtime

Posted on 2006-06-14
8
644 Views
Last Modified: 2010-04-05
Hi.

How can I add a calculated field to a TQuery at runtime?

Thanks a lot.

Rogerio
0
Comment
Question by:rogerbrito
  • 6
  • 2
8 Comments
 
LVL 11

Expert Comment

by:calinutz
ID: 16905604
Your question is strange...
Calculated fields in a query can be added through "Select" SQL statement like:

select appleKind, count(*) from fruits group by applekind

(above you have a normal field and a calculated field)

Maybe your question is how to add a calculated field into a DBGrid that displays the result of a Query...

Please rephrase your question.

0
 
LVL 11

Expert Comment

by:calinutz
ID: 16905647
To add a column to a DBGrid in runtime use this:

DBGrid1.Columns.Add.Title.Caption:='calculated field';

Regards
0
 
LVL 11

Expert Comment

by:calinutz
ID: 16905799
Sorry... I missunderstood your problem.  For runtime calculated fields check out these examples in PAQs (they refere to Table calculated fields but I guess there will not be too much difference between the two cases);

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20459093.html
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10093332.html
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20289498.html

Regards
0
 

Author Comment

by:rogerbrito
ID: 16905811
Hi calinutz

And how do I populate the new column on the dbgrid?

Thank you
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:calinutz
ID: 16905848
0
 
LVL 11

Accepted Solution

by:
calinutz earned 125 total points
ID: 16905909
If you only use the Grid for displaying... you could choose to draw the text you need on the Cerll in the OnDrawCollumnCell Event using DefaultDrawColumnCell method ... plenty of examples out there on google

But remember... they will be drawings... not real data, so you won't be able to use the data drawn there as you use the data returned from the query...
I do not understand why don't you calculate the calculated field inside the SQL... this way you won't need these complications.

Cheers
0
 
LVL 11

Expert Comment

by:calinutz
ID: 16905959
If it is too complicated to calculate it in a simple query... then complicate your query:
Use a temporary table that you generate upon starting the calculations... create as many columns in it as you like. Then populate it with inserts and updates until you obtain the needed result in a table then just display it in a DBGrid.
Do the above either in a Stored Procedure or (worst case) in delphi code using SQLCommands or even Query.ExecSQL for executing a create table, an update table and so on...
No big deal... Why complicating it ?

Regards
0
 

Author Comment

by:rogerbrito
ID: 16906087
Yes, the grid is only for displaying.
I have a column that display a status code, like S1, S2, S3, S4, etc. I want to show a friendly description instead of the Status Code on the datagrid.

I guess drawing the text on a new column will do. I'll make some tests.

Thank you
Rogerio
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now