* * *

Author Topic: How to replace the field type and value in dbgrid?  (Read 3034 times)

ezlage

  • Sr. Member
  • ****
  • Posts: 280
  • (Silêncio)
    • Lageteck
Re: How to replace the field type and value in dbgrid?
« Reply #15 on: April 12, 2018, 10:35:04 pm »
Friend, take a look at the attachment.

Code: Pascal  [Select]
  1. procedure TForm1.ZQuery1newfieldGetText(Sender: TField; var aText: string;
  2.   DisplayText: Boolean);
  3. begin
  4.   case AnotherField.AsInteger of  //refer to the integer field
  5.     0: AText:='Zero';
  6.     1: AText:='Um';
  7.     2: AText:='Dois';
  8.     else AText:='Qualquer coisa';
  9.   end;
  10. end;
« Last Edit: April 12, 2018, 11:03:34 pm by ezlage »
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

vrull

  • Full Member
  • ***
  • Posts: 109
Re: How to replace the field type and value in dbgrid?
« Reply #16 on: April 13, 2018, 02:06:23 am »
@Vodnik,
If you can modify the database, it is easier to make a lookup table to keep all your Ids with user-readable descriptions.
The point is if you work with SQL you can always construct a query which would give you all data you need. You can run that query independently from your program and make sure all your results are correct.
Then you make a program which just reads the result of that query and presents data to the end user; it formats data to make it look nice, but does not modifies anything.
With this approach you separate user interface from data processing, you know where to look if something went wrong. SQL is very powerful language, it can do almost everything with you data.

Vodnik

  • New member
  • *
  • Posts: 38
Re: How to replace the field type and value in dbgrid?
« Reply #17 on: April 13, 2018, 10:54:49 pm »
Friend, take a look at the attachment.
ezlage, I'm using this, thanks!

Vodnik

  • New member
  • *
  • Posts: 38
Re: How to replace the field type and value in dbgrid?
« Reply #18 on: April 13, 2018, 11:15:55 pm »
@Vodnik,
If you can modify the database, it is easier to make a lookup table to keep all your Ids with user-readable descriptions.
The point is if you work with SQL you can always construct a query which would give you all data you need. You can run that query independently from your program and make sure all your results are correct.
Then you make a program which just reads the result of that query and presents data to the end user; it formats data to make it look nice, but does not modifies anything.
With this approach you separate user interface from data processing, you know where to look if something went wrong. SQL is very powerful language, it can do almost everything with you data.
@vrull, I can't. The SW product that is using this DB restricts changing DB. It also restricts querying more than 10000 records at a time. And is very sensible to table locking time. Known practice is to create a replication to another DB and work with it, but I'm not yet good enough to do this. Then IDs can be replaced with text. BUT: More bytes -> more space -> more memory -> more processing time, isn't it?
I'm the beginner both in SQL and Pascal, and still thinking about giving all the work to SQL. As I have already posted, having a CASE statement with 50 variants discourages me. Also some SQL constructions that I have tried are not supported in Lazarus.
« Last Edit: April 13, 2018, 11:20:13 pm by Vodnik »

vrull

  • Full Member
  • ***
  • Posts: 109
Re: How to replace the field type and value in dbgrid?
« Reply #19 on: April 17, 2018, 06:51:39 am »
@Vodnik. You have not stated what database you are working with. If it is powerful enough, like MS SQL Server, there is lot of ways to achieve desired results. You client application can run a query at start-up to make temporary lookup tables; it may be a case statement with 50 options, but you run it only once and use as long as you connection is active.
Another suggestion is to separate Pascal code from any other languages, be it SQL, HTML, XML or what ever else you program may work with. This makes life much easier.

Seems to be running off-topic.

Vodnik

  • New member
  • *
  • Posts: 38
Re: How to replace the field type and value in dbgrid?
« Reply #20 on: April 17, 2018, 06:49:10 pm »
@vrull, I work with Informix DB via ODBC (because there is no direct connection in Lazarus). It is very powerful!
The things you are talking about I would like to learn, but do not know how - not so much is documented for Lazarus.

vrull

  • Full Member
  • ***
  • Posts: 109
Re: How to replace the field type and value in dbgrid?
« Reply #21 on: April 18, 2018, 01:09:05 am »
@Vodnik,
This is more like the concept rather that specific Lazarus features.
You can have a look at this project which implements this concept to some extend:
https://sourceforge.net/projects/sqlmm/
The Lazarus code is purely the user interface, It does know anything about data apart from the fact it should connect to an SQLite3 database (in my case). It keeps everything, which is not Pascal code, in a special container (an XML file in that case; you can see it using my XML Tree Editor https://sourceforge.net/projects/xmltreeeditor).
So you
TSQLQuery(yourComponent).SQL.Text := <read the whole query from the container>;
<set query parameters from user input or other sources>;
Open or ExecSQL to run.
You can design you query in a "Management Studio", make sure it works as expected and gives you all result you need. And you can apply all the power of SQL Engine to avoid messing up with data in Pascal code.

Certainly, this is my own opinion, you don't have to follow.



 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus