Recent

Author Topic: DBGrid formatting for column with a date  (Read 992 times)

mcculloch

  • New member
  • *
  • Posts: 6
DBGrid formatting for column with a date
« on: November 28, 2019, 02:40:11 am »
I'd like to format two date columns in a TDBGrid.  When I set the display format for the columns, it applies the format successfully to the design time columns.  The run time columns are different than the default design format and my defined format.  How do I set the displayformat that is used at runtime?  I have a format on a float that works fine at design time and runtime.

fmc

  • New Member
  • *
  • Posts: 37
Re: DBGrid formatting for column with a date
« Reply #1 on: November 28, 2019, 03:01:23 am »
Add two Columns to the DBGrid using your Object Inspector.  Select the ellipses in the Column properties, then add two Title fields. Notice above the Object Inspector two Column properties showed up in the Component Tree.

Click the first one in that tree. Then in your Object Inspector, set the DisplayFormat to yyyy/mm/dd (or however you want it to look), then set the FieldName property to one of your DateFields. Next, scroll down to Title, expand it, set the Caption you want on the TitleBar of your DBGrid.
« Last Edit: November 28, 2019, 03:11:13 am by fmc »
Win X Pro / Lazarus 2.0.6 / FPC 3.0.4

mcculloch

  • New member
  • *
  • Posts: 6
Re: DBGrid formatting for column with a date
« Reply #2 on: November 28, 2019, 03:17:01 am »
Thanks but that's where I am now.  I have all of that but it doesn't apply the same format at run time that it applies at design time.

fmc

  • New Member
  • *
  • Posts: 37
Re: DBGrid formatting for column with a date
« Reply #3 on: November 28, 2019, 03:32:49 am »
I don't know what to say then. I just tested a DateField with yyyy/mm/dd and mm/dd/yyyy and dd/mm/yyyy and they all displayed as per the DisplayFormat. Not sure what you have going on.
« Last Edit: November 28, 2019, 03:41:26 am by fmc »
Win X Pro / Lazarus 2.0.6 / FPC 3.0.4

valdir.marcos

  • Hero Member
  • *****
  • Posts: 965
Re: DBGrid formatting for column with a date
« Reply #4 on: November 28, 2019, 11:02:26 am »
I'd like to format two date columns in a TDBGrid.  When I set the display format for the columns, it applies the format successfully to the design time columns.  The run time columns are different than the default design format and my defined format.  How do I set the displayformat that is used at runtime?  I have a format on a float that works fine at design time and runtime.
Add two Columns to the DBGrid using your Object Inspector.  Select the ellipses in the Column properties, then add two Title fields. Notice above the Object Inspector two Column properties showed up in the Component Tree.
Click the first one in that tree. Then in your Object Inspector, set the DisplayFormat to yyyy/mm/dd (or however you want it to look), then set the FieldName property to one of your DateFields. Next, scroll down to Title, expand it, set the Caption you want on the TitleBar of your DBGrid.
Thanks but that's where I am now.  I have all of that but it doesn't apply the same format at run time that it applies at design time.
I don't know what to say then. I just tested a DateField with yyyy/mm/dd and mm/dd/yyyy and dd/mm/yyyy and they all displayed as per the DisplayFormat. Not sure what you have going on.
Please, prepare and attach a small sample project and also attach a image of that date format at design time and another image of the same date format at run time.
It's difficult to help you without understand what you are really going through.

mcculloch

  • New member
  • *
  • Posts: 6
Re: DBGrid formatting for column with a date
« Reply #5 on: November 29, 2019, 12:53:15 am »
I made a zip file with the project and a screen shot.  I left out the lib and backup directory and it was still too big so I left out the screen shot.  (250K limit?  Really?)  All the screen shot showed was a design time window and a runtime window showing different formats for the date columns.  Let me know if you need to see the screen shot.  It's still too big even all by itself so I'll have to figure out something else.

winni

  • Hero Member
  • *****
  • Posts: 717
Re: DBGrid formatting for column with a date
« Reply #6 on: November 29, 2019, 01:13:10 am »
Hi !

I can't compile the project due to missing libs, but:

dbGridSecurities --> Object Inspector --> Options

dgColumResize is enabled. Disable it.

Perhaps this is just the reason.

Winni

valdir.marcos

  • Hero Member
  • *****
  • Posts: 965
Re: DBGrid formatting for column with a date
« Reply #7 on: November 29, 2019, 01:08:29 pm »
I made a zip file with the project and a screen shot.  I left out the lib and backup directory and it was still too big so I left out the screen shot.  (250K limit?  Really?)  All the screen shot showed was a design time window and a runtime window showing different formats for the date columns.  Let me know if you need to see the screen shot.  It's still too big even all by itself so I'll have to figure out something else.
I have opened you small sample project and to my surprise there is no sample database file to test it and you misplaced the database components outside of the obvious visible screen.
Please, check the attached images.

Looking to your "unit_securities.lfm", why have you set DisplayFormat on TDBGrid instead of on TSQLQuery?
Please, first remove DisplayFormat information from TDBGrid, then add it to TSQLQuery fields and test your project again both on design and run time.
Code: Pascal  [Select]
  1. object FormSecurities: TFormSecurities
  2.   Left = -34
  3.   Height = 384
  4.   Top = 188
  5.   Width = 1028
  6.   Caption = 'Securities'
  7.   ClientHeight = 384
  8.   ClientWidth = 1028
  9.   LCLVersion = '2.0.7.0'
  10.   object dbgridSecurities: TDBGrid
  11.     Left = 64
  12.     Height = 166
  13.     Top = 72
  14.     Width = 967
  15.     Color = clWindow
  16.     Columns = <    
  17.       item
  18.         Title.Caption = 'Symbol'
  19.         FieldName = 'symbol'
  20.       end    
  21.       item
  22.         Title.Caption = 'Description'
  23.         Width = 350
  24.         FieldName = 'description'
  25.       end    
  26.       item
  27.         Title.Caption = 'Ex-dividend Date'
  28.         FieldName = 'ex_div_date'
  29.         **********************************
  30.         **  DisplayFormat = 'mm/dd/yy'  **
  31.         **********************************
  32.       end    
  33.       item
  34.         Title.Caption = 'Dividend Date'
  35.         FieldName = 'div_date'
  36.         DisplayFormat = 'mm/dd/yy'
  37.       end    
  38.       item
  39.         Title.Caption = 'Yield'
  40.         FieldName = 'yield'
  41.         DisplayFormat = '0.00%'
  42.       end>
  43.     DataSource = dataSecurities
  44.     TabOrder = 0
  45.   end
  46.   object sqlconnSecurities: TMySQL57Connection
  47.     Connected = False
  48.     LoginPrompt = False
  49.     DatabaseName = 'investments'
  50.     KeepConnection = False
  51.     Password = 'JWM&Bear'
  52.     Transaction = sqltransSecurities
  53.     UserName = 'john'
  54.     HostName = 'server01.local'
  55.     SkipLibraryVersionCheck = False
  56.     Left = 72
  57.     Top = 757
  58.   end
  59.   object sqltransSecurities: TSQLTransaction
  60.     Active = False
  61.     Database = sqlconnSecurities
  62.     Left = 200
  63.     Top = 757
  64.   end
  65.   object sqlqrySecurities: TSQLQuery
  66.     IndexName = 'DEFAULT_ORDER'
  67.     FieldDefs = <    
  68.       item
  69.         Name = 'symbol'
  70.         DataType = ftString
  71.         Precision = -1
  72.         Size = 12
  73.       end    
  74.       item
  75.         Name = 'description'
  76.         DataType = ftString
  77.         Precision = -1
  78.         Size = 60
  79.       end    
  80.       item
  81.         Name = 'ex_div_date'
  82.         DataType = ftDate
  83.         Precision = -1
  84.         **********************************
  85.         **  DisplayFormat = nothing     **
  86.         **********************************
  87.       end    
  88.       item
  89.         Name = 'div_date'
  90.         DataType = ftDate
  91.         Precision = -1
  92.       end    
  93.       item
  94.         Name = 'yield'
  95.         DataType = ftFloat
  96.         Precision = -1
  97.       end>
  98.     Database = sqlconnSecurities
  99.     Transaction = sqltransSecurities
  100.     SQL.Strings = (
  101.       'SELECT * FROM securities ORDER BY symbol'
  102.     )
  103.     Params = <>
  104.     Left = 352
  105.     Top = 760
  106.   end
  107.   object dataSecurities: TDataSource
  108.     DataSet = sqlqrySecurities
  109.     Left = 496
  110.     Top = 757
  111.   end
  112. end

mcculloch

  • New member
  • *
  • Posts: 6
Re: DBGrid formatting for column with a date
« Reply #8 on: November 29, 2019, 02:35:08 pm »
I just went with what I saw in the object inspector.  There's no such property on the object inspector where you show "DisplayFormat = nothing".  Under Precision = -1 there is only Size = 0.  Should I edit .lfm file directly?  How do I add this?

And there is no sample database file since the database I'm using is not a file.  I'm using MySQL.   Also, the database components may be off the window since I attempted to fit both the design time and run time window on the screen for the screen shot which unfortunately is too big to attach.

Please don't waste any more time on this.  I only found Lazarus about a month ago after looking for an open source analog of Delphi for PHP.  I had Delphi back in the early '90s and found it great for small projects.  I learned Pascal in college starting in 1980 and thought that Borland had done great things with Turbo Pascal for Windows around 1990 which became Delphi soon after.  I am really more interested in web for user interface.  That's why I was looking for open source alternatives to Delphi for PHP. 

Thanks to all who tried to help.
« Last Edit: November 30, 2019, 03:58:33 am by mcculloch »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 965
Re: DBGrid formatting for column with a date
« Reply #9 on: November 30, 2019, 06:18:10 am »
I just went with what I saw in the object inspector.  There's no such property on the object inspector where you show "DisplayFormat = nothing".  Under Precision = -1 there is only Size = 0.  Should I edit .lfm file directly?  How do I add this?
Please, check the attached image.

5. Right click on the SQLQuery component to access the field editor;
6. Right click on the field editor blank area to add one, some or all fields;
7. Click on Create button;
8. Click on the chosen field, then you will see the DisplayFormat properties for numeric fields.


More information:

Data controls based on the field editor...
https://forum.lazarus.freepascal.org/index.php?topic=40748.0

How to replace the field type and value in dbgrid?
https://forum.lazarus.freepascal.org/index.php/topic,40674.0.html

Drag and drop from the fields editor to the form to dynamically create fields on form at design time.
https://bugs.freepascal.org/view.php?id=33555
Quote
And there is no sample database file since the database I'm using is not a file.  I'm using MySQL.
Providing an SQL script would solve that.
For instance:
Code: SQL  [Select]
  1. CREATE TABLE xyz
  2. (
  3.   id       INTEGER PRIMARY KEY,
  4.   name VARCHAR(40)
  5. );
  6. commit;
  7.  
  8. INSERT INTO xyz(name) VALUES('abc');
  9. commit;
Quote
Also, the database components may be off the window since I attempted to fit both the design time and run time window on the screen for the screen shot which unfortunately is too big to attach.
Internet is plenty of sites to shrink image sizes.
For example:
https://www.easy-resize.com/
Quote
Please don't waste any more time on this.
I only found Lazarus about a month ago after looking for an open source analog of Delphi for PHP.
As far as I can remember,  CodeGear Delphi for PHP 1.0 was released in 2007 and died soon after as Embarcadero RadPHP XE2 in 2011.
https://edn.embarcadero.com/article/34059
https://en.wikipedia.org/wiki/Delphi_(software)
https://delphi.fandom.com/wiki/Delphi_for_PHP
http://docwiki.embarcadero.com/RadPHP/en/Main_Page
https://support.embarcadero.com/article/38899
https://www.wikiwand.com/en/Delphi_(software)
https://en.wikipedia.org/wiki/RadPHP

Quote
I had Delphi back in the early '90s and found it great for small projects.
I learned Pascal in college starting in 1980 and thought that Borland had done great things with Turbo Pascal for Windows around 1990 which became Delphi soon after.
I am really more interested in web for user interface.
That's why I was looking for open source alternatives to Delphi for PHP.
Whatever you need, there are alternatives using Free Pascal and Lazarus:

The future of the Lazarus IDE
https://forum.lazarus.freepascal.org/index.php/topic,47506.0.html

Modern UI in Lazarus
https://forum.lazarus.freepascal.org/index.php/topic,36502.msg339987.html#msg339987

Create Web App
https://forum.lazarus.freepascal.org/index.php/topic,47439.0.html

WebServices on Lazarus/FPC
https://forum.lazarus.freepascal.org/index.php/topic,47132.0.html

handling https with managed lazarus http server
https://forum.lazarus.freepascal.org/index.php/topic,46906.0.html

TMS Web Core vs. Elevate Web Builder vs, FMSoft UniGUI vs. VirtualUI vs. ?
https://forum.lazarus.freepascal.org/index.php/topic,44636.0.html
Quote
Thanks to all who tried to help.
You're welcome.

mcculloch

  • New member
  • *
  • Posts: 6
Re: DBGrid formatting for column with a date
« Reply #10 on: December 01, 2019, 10:40:05 pm »
I was going to give up on this but your answers were so good I gave your suggestions one more attempt.  When I add the field to the query object, it adds them "along side" of the FieldDefs array I already had.  So I didn't understand the difference between the fields in the FieldDefs property and the ones using your method.  But, much more importantly, just after I do that, if I click on anything in the object inspector I get access violations and I have to abort the program.  I'm using version 1.8.2 under Ubuntu 18.04.

I took a screen shot of my project after adding the fields, showing both sets of field definitions in both places.  Unfortunately, it won't compress small enough to post.  Either way, I'm done now.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 965
Re: DBGrid formatting for column with a date
« Reply #11 on: December 02, 2019, 02:33:38 am »
I was going to give up on this but your answers were so good I gave your suggestions one more attempt.
Thank you. We are trying to help you.
Quote
When I add the field to the query object, it adds them "along side" of the FieldDefs array I already had. 
So I didn't understand the difference between the fields in the FieldDefs property and the ones using your method.
But, much more importantly, just after I do that, if I click on anything in the object inspector I get access violations and I have to abort the program.
It's difficult to understand your problem without a small sample project that shows those error messages you are going through.
Quote
I'm using version 1.8.2 under Ubuntu 18.04.
I took a screen shot of my project after adding the fields, showing both sets of field definitions in both places.
Unfortunately, it won't compress small enough to post.
Either way, I'm done now.
My suggestion is: open your mind to a new world.
Just for a moment, stop what you are doing, and get yourself some time to learn a new way of doing things:
https://wiki.freepascal.org/Databases
https://www.youtube.com/results?search_query=free+pascal+lazarus

After that review, start a new project from scratch to test what you have just learned.
In case of new problems, attach a small sample project so we can better help you.

mcculloch

  • New member
  • *
  • Posts: 6
Re: DBGrid formatting for column with a date
« Reply #12 on: December 02, 2019, 02:00:09 pm »
My suggestion to whomever maintains this forum is to raise the attachment size limit.

wp

  • Hero Member
  • *****
  • Posts: 6654
Re: DBGrid formatting for column with a date
« Reply #13 on: December 02, 2019, 03:38:45 pm »
My suggestion to whomever maintains this forum is to raise the attachment size limit.
(I am not the forum maintainer, but:) Don't include compiler-generated files in the attachment. For source code projects (and this is why attachments are good for here) it is mostly sufficient to include the *.pas, *.lfm, *.lpi and .lpr files plus maybe data files. Dont't include the generated binary and ppu, o, res, ico etc files. This way the zipped size will stay well below the 250 kB.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10