Solved

Add a calculated field to a TQuery at runtime

Posted on 2006-06-14
8
643 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

19 Experts available now in Live!

Get 1:1 Help Now