• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3121
  • Last Modified:

Change Date format in DBGrid

Hi

I have a DBGRID hooked up to a datasource with .NET datetime fields.

The dates are displaying as "MM/DD/YYYY 00:00:00"

How can I get rid of the time part in the grid display?

Thanks
0
zattz
Asked:
zattz
  • 5
  • 4
  • 3
  • +1
1 Solution
 
Geert GruwezOracle dbaCommented:
set the displayformat property of the dataset (query or table) to mm/dd/yyyy

you have to add the fields to the dataset, by right clicking on the component in the form editor
and select add all fields

or
procedure TForm1.FormCreate(Sender: TObject);
begin
  TDateTimeField(Query1.FieldByName('DATECOLUMN')).DisplayFormat := 'MM/DD/YYYY');
end;
0
 
rfwoolfCommented:
that's a good way. another way is to add your fields as persistent fields in the dataset (as Geert instructs) and then use the onsettext for the relevant field.
0
 
zattzAuthor Commented:
I am using the dbgrid in vcl.net and there is no displayformat for the columns
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Geert GruwezOracle dbaCommented:
not on the dbgrid no, it's on the dataset component ...
and only for TDateTime Fields !

like
TDateTimeField(TQuery.FieldByName('FIELDNAME')).DisplayFormat

the ordinary TField doesn't have the property, it must be cast to TDateTimeField
0
 
senadCommented:
Another approach is this :
Usually people enter data into the grid (insert) by the use of the DateTimePicker.
So do it over there..Set format over there..
cxDateEdit has SaveTime and ShowTime options
0
 
Geert GruwezOracle dbaCommented:
this is for a DBGrid not a DevExpress Grid ...
0
 
zattzAuthor Commented:
I am using delphi.net so the type is DateTime and not TDateTime.

Still have not resolved the issue
0
 
rfwoolfCommented:
zattz> Go to your dataset, double-click it, a new window pops up. Right click in the window and select Add All Fields. You've just made the fields of your dataset "persistent". Click on the relevant field (e.g. "MyDateField") and in the object inspector look under displayFormat.
sigh...
0
 
Geert GruwezOracle dbaCommented:
how long have you been using Delphi (.Net has nothing to do with this ...)

or read this first:
http://veerle-en-geert.be/delphi/ebooks/Delphi%20-%20Teach%20Yourself%20Borland%20Delphi%204%20in%2021%20Days.pdf

specially as from chapter 16
0
 
zattzAuthor Commented:
rfwoolf, it is a third party dataset that does not work the way the old VCL ones did, which is why I wanted to do it when the grid draws.

0
 
rfwoolfCommented:
zattz> Well I think you can technically use the grid drawing - worth a shot.
Here's a link to an about.com tutorial on using color in the DBgrid. What we're going to be doing instead is using this same technology but changing the values of the cells...
http://delphi.about.com/od/usedbvcl/l/aa031699.htm

I don't have delphi open so you may have to figure this out yourself, but basically, in your OnDrawColumnCell even of the DBGrid, having anything in there will overwrite the default drawing procedure of the DBGrid. Once you've done that just make sure you call DefaultDrawColumnCell in such a way that instead of putting the normal text of the field, it will put a formatted text of the field.
Here's an example to get you started.
Later when I'm feeling better I might open delphi and try figure this out for you.

procedure TForm1.DBGrid1DrawColumnCell
   (Sender: TObject; const Rect: TRect;
    DataCol: Integer; Column: TColumn;
    State: TGridDrawState);
begin
DBGrid1.DefaultDrawColumnCell
  (Rect, DataCol, Column, State);
end;

Open in new window

0
 
Geert GruwezOracle dbaCommented:
which 3rd party dataset is it ?
0
 
rfwoolfCommented:
zattz> I should add about my procedure is defaultdrawcolumncell must be normal for all cells and columns except for the date one, so you should in fact put an IF THEN in there along these lines:
if Column = Table1.Fieldbyname('MyDate') then
  //dbgrid1.DefaultDrawColumnCell(SOMETHING)
else
  dbgrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now