I have a column in my TAdvStringGrid that are dates. I'd like the column to be displayed as
Thu 04 Dec 2025
so I implement the OnGetDisplText event. But it seems like this DisplText is also used in the .Date[Col,Row] property instead of the Cell content itself (which I don't care about).
How do I manage to have a TDate column with the above (or any other) custom DISPLAY text and still use the drop-down Calendar to edit the cell value?
GetDisplText should (IMO) ONLY be used to get the DISPLAY text. No other use, ie. not for validation, initial value input to editor, whatever. ONLY to specify the text to be DISPLAYED in the cell.
But that is probably too late to change. I therefore suggest an Event (or PROTECTED VIRTUAL method, so I can override it in my own implementation) that handles the actual Display of the cell text when not in editor mode.
This code applied to a default TAdvStringGrid shows how you can do this:
procedure TForm1.AdvStringGrid1GetDisplText(Sender: TObject; ACol,
ARow: Integer; var Value: string);
var
dt: TDateTime;
begin
if (acol = 1) and (Arow > 0) then
begin
dt := StrToDate(Value);
value := formatdatetime('DDD dd MMM YYYY', dt);
end;
end;
procedure TForm1.AdvStringGrid1GetFormat(Sender: TObject; ACol: Integer;
var AStyle: TSortStyle; var aPrefix, aSuffix: string);
begin
if acol = 1 then
AStyle := ssDate;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
for i := 1 to AdvStringGrid1.RowCount - 1 do
advstringgrid1.Dates[1,i] := Now + random(30);
AdvStringGrid1.SortSettings.Show := true;
AdvStringGrid1.SortSettings.SortOnVirtualCells := false;
AdvStringGrid1.Options := AdvStringGrid1.Options + [goEditing];
AdvStringGrid1.DefaultEditor := edDateEdit;
end;
Thank you - I'll check it out and get back to you. But I'm using other editors in other columns (TCheckBox, TEdit, TComboBox etc.), so a "defaulteditor" is not appropriate, I think. And the "Value" assignment in GetDIsplText is also the one that is used (in my tests) when the DateEditor is being displayed (leading to "Thu 04 Dec 2025 is not a valid Date Specifier" (or something along those lines) when it is passed on to StrToDate at some point).
Are there any of the settings done in FormCreate that circumvents this? I do not use "SortSettings" at all, and use an event for Edit availability (only some columns are editable).
I used DefaultEditor to save time to not write the OnGetEditorType event handler.
This could have been
procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; ACol,
ARow: Integer; var AEditor: TEditorType);
begin
if ACol = 1 then
AEditor := edDateEdit;
end;
With the test code provided, I don't see an issue to start the editing. The cell has here
'di 23 dec' and it starts the inplace editor on this date:
Please try to the test code and compare with how you use it.
I'll check it out tomorrow and get back to you...
Okay - it's a little more complicated. I have hooked up on OnDatePickerCloseUp. In that event, I end up calling Grid.Dates[Col,Row] and that in turns ends up calling GetDisplText where I supply the modified string (not the cell value) and that string is then passed into StrToDate which it of course doesn't understand.
The "Dates" property should not end up calling GetDisplText - it should simply use what is in the Cell.
If you want the unformatted cell value, use grid.GridCells[col,row]
Yes, but I want the date stored in there. So this:
function TAdvStringGrid.GetDates(ACol,ARow: Integer):TDateTime;
begin
GetDates := StrToDate(Cells[ACol,ARow]);
end;
should be
function TAdvStringGrid.GetDates(ACol,ARow: Integer):TDateTime;
begin
GetDates := StrToDate(GridCells[ACol,ARow]);
end;
We will reflect on this and investigate whether this won't cause breaking changes for other users. Please meanwhile use GridCells directly at application level.