Solved

Add a calculated field to a TQuery at runtime

Posted on 2006-06-14
8
647 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rogerbrito
ID: 16905811
Hi calinutz

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

Thank you
0
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 120
DBCtrlGrid, Delphi, Scroll 8 55
Firemonkey Queue list 1 25
Using MMsystem To change audio input 1 14
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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