ISO 8601 string date picker (Delphi, DevExpress)

1.2k Views Asked by At

Our Delphi application uses database-bound TcxGridDBColumns to let the user manipulate a ISO 8601-formatted date (YYYY-MM-DD) as a string. I would like to offer the end user a calender-based date picker.

TcxDateEditProperties can't be used (directly) since the underlying database uses a string field. So to my understanding I'm left with the options to

  1. Create an additional date column (TDateTime DB field) in all tables and convert the date to the ISO 8601 string column on the BeforePost event of TDataSet

  2. Create a custom Tcx***Properties class. This would likely involve inheriting from TcxPopupEditProperties.

Since there are many tables affected, I would much rather use #2. Can you point to help documents helping me with that? Or is there a #3?

2

There are 2 best solutions below

1
On

if what you mean by documents is about writing custom component, then you can see here Creating Custom Delphi Components, as well as the document 'Component Writer's Guide' pdf that comes with Delphi (it has example about customizing DBGrid and navigating months, year and days too). This can be used for starting point for option no.2

0
On

There is a #3: No extra column needed, DateEdit understands strings in both current system format (in which a user change is written back) and ISO format. In the BeforePost I convert the system format back to the ISO which I need in the database. As an extra luxury, the following event changes the existing ISO dates to what the user's default date format:

procedure T_RVVorbereitung.cxGridAnalysisDateGetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
var
  ADate                                 : TDateTime;
begin
  try
    try
      ADate := TIso8601.DateTimeFromIso8601(AText);         // YYYY-MM-DD
    except
      ADate := StrToDate(AText);
    end;
    AText := DateToStr(ADate);
  except
    // neither system nor ISO date: show as it is
  end;
end;