We help IT Professionals succeed at work.

Change Date format in DBGrid

zattz
zattz asked
on
Medium Priority
4,815 Views
Last Modified: 2013-11-23
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
Comment
Watch Question

Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
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;

Commented:
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.

Author

Commented:
I am using the dbgrid in vcl.net and there is no displayformat for the columns
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
this is for a DBGrid not a DevExpress Grid ...

Author

Commented:
I am using delphi.net so the type is DateTime and not TDateTime.

Still have not resolved the issue

Commented:
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...
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

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.

Commented:
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

Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
which 3rd party dataset is it ?
Commented:
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)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.