Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dbGrid 2

Status
Not open for further replies.

michaenh

Programmer
Aug 7, 2002
205
NO
Hi experts.

Well I have a dbGrid which I get some data from my SQL database.
The problem is one of the field retrieves DateTime.

I only want dbGrid to show the date value in that field, not the whole value.

What should I do? I Could make heavy SQL sentence..or is it better ways?

ex. how I get data to dbgrid (exports knows it)
Datamodul.MyADOQuery.SQL.Text := 'bla..bla'
Datamodul.MyADOQuery.Active := True;
Datamodul.MyDataSource.DataSet := Datamodul.MyADOQuery;
DBGrid1.DataSource := Datamodul.MyDataSource;

cheers,
mha
 
Maybe I should use TStringGrid instead to fill data to each cell(rows and columns), but then I have to code more. Benefits I will have more controll over the grid. Any ideas?

cheers,
mha

 
You should still use TDBGrid.

Use the GetText event handler for the field in question. In the handler you could use FormatDateTime to get the date format that you want. For example, if the field is called WatchTime your event handler might look like this:
Code:
procedure TForm1.WatchTimeGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  text := FormatDateTime ( 'dd/mm/yyyy', Sender.AsDateTime );
end;

Hope this helps.

Andrew
 
Thanks for the help.

Got it!... smile! thanks

cheers,
mha
 
Two small caveates with Andrew's code:

1. Always prepare for a null value.
2. Use the user's (or application's) date format.

Code:
procedure TForm1.WatchTimeGetText(Sender: TField; var Text: String; DisplayText: Boolean);
begin
   if not Sender.IsNull then
       Text := DateToStr(Sender.AsDateTime);
end;

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top