Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Add a calculated field to a TQuery at runtime

Posted on 2006-06-14
8
Medium Priority
?
651 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rogerbrito
ID: 16905811
Hi calinutz

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

Thank you
0
 
LVL 11

Accepted Solution

by:
calinutz earned 375 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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