Solved

Add a calculated field to a TQuery at runtime

Posted on 2006-06-14
8
646 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

861 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