Avatar of ejla51
ejla51
 asked on

Change Time format on DBGrid

I have a database table (TkbmMemTable) and stored time in the table is format "hh:mm:ss" but I want to format this to "hhmm" or "hh:mm" on DBGrids Time column...
How to do?
Delphi

Avatar of undefined
Last Comment
ejla51

8/22/2022 - Mon
Sinisa Vuk

set display format on TField:

TDateTimeField(dbTable.FieldByName('field1')).DisplayFormat:='hh:mm';

Open in new window

ejla51

ASKER
Ok... sorry, but my Time field is of String type  :-(
Is there some way to set format?
Sinisa Vuk

Then you must reformat it by "hand". Set new event OnText to field:

... //somwhere on form create event
dbTable.FieldByName('field1').onText := tblTable1GetText;
...
procedure TForm.tblTable1GetText(Sender: TField; var Text: string;
    DisplayText: Boolean);
begin
  if Sender.FieldName='field1' then
  begin
     Text:=Copy(Sender.AsString, 1, 5); //or you can parse to values form EncodeTime
  end;
end;

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
ejla51

ASKER
>dbTable.FieldByName('field1').onText := tblTable1GetText.

Thanks, but I can't found where put this line.
Have tried to put in Form.OnCreate and many other places too :-)
Compiler says "Undeclared identifier onText"
My table is located in Datamodule.
ASKER CERTIFIED SOLUTION
Sinisa Vuk

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ejla51

ASKER
Sorry, but "Undeclared identifier onText" still appers!
ejla51

ASKER
>dbTable.FieldByName('field1').onText := tblTable1GetText
Is this really correct syntax -
 and not dbTable.FieldByName('field1').onGetText := tblTable1GetText instead?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sinisa Vuk

Sorry, can be, depends on what db component you use, ado , bde, db express,... in design time look for OnText or OnGetText event in query and do double click to get procedure definition and put code there.
ejla51

ASKER
I got it work at last...

- Moved OnGetText redirection after loading of binary file in MemTable. Has been in the DatamoduleCreate before.
- Memtable.Refresh; (was not there before)
- DBGrid.Refresh (needed too  - otherwise mouseclick on Grid refresh display format)

Thanks!
//EL