Link to home
Start Free TrialLog in
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?
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

set display format on TField:

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

Open in new window

Avatar of ejla51
ejla51

ASKER

Ok... sorry, but my Time field is of String type  :-(
Is there some way to set format?
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

Avatar of 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
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ejla51

ASKER

Sorry, but "Undeclared identifier onText" still appers!
Avatar of ejla51

ASKER

>dbTable.FieldByName('field1').onText := tblTable1GetText
Is this really correct syntax -
 and not dbTable.FieldByName('field1').onGetText := tblTable1GetText instead?
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.
Avatar of 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