TMSFNCDataGrid Date column show Time

I want my DataGrid to show Dates without Time Information.
So i Try following:

{ -------------------------------------------------------------------------------
  DatabaseAdapter Datentyp pro Spalte setzen (FieldToData Event)
  Nachdem das Grid aufgebaut wurde, setzt die Typen der Spalten (Integer,Date,Boolean) Rest ist String
  ------------------------------------------------------------------------------- }
procedure TfrmEinstellungen.GridDatabaseAdapterEFieldToData(Sender: TObject; ACell: TTMSFNCDataGridCellCoord;
  AMasterField, AField: TField; var ACellValue: TTMSFNCDataGridCellValue; var Handled: Boolean);
begin
  // Integer Felder
  if (AField.FieldName = 'KdNr') then
  begin
    ACellValue := AField.AsInteger;
    Handled := True;
  end;

  // Datums Felder
  if (AField.FieldName = 'GeburtsDatum') or (AField.FieldName = 'EingabeDatum') then
  begin
    ACellValue := AField.AsDateTime;
    Handled := True;
  end;

  // Boolean Felder
  if (AField.FieldName = 'Passiv') then
  begin
    ACellValue := AField.AsBoolean;
    Handled := True;
  end;
end;

{ -------------------------------------------------------------------------------
  Grid GetCellFormatting Event
  Alle Spalten vom Typ DateTime sollen im Format dd.MM.yy Formatiert werden
  ------------------------------------------------------------------------------- }
procedure TfrmEinstellungen.grdTestGetCellFormatting(Sender: TObject; ACell: TTMSFNCDataGridCellCoord;
  AData: TTMSFNCDataGridCellValue; var AFormatting: TTMSFNCDataGridDataFormatting;
  var AConvertSettings: TFormatSettings; var ACanFormat: Boolean);
var
  Field: TField;
begin
  // Überprüfen, ob die Zelle in einer DateTime-Spalte ist
  Field := GridDatabaseAdapterE.Columns[ACell.Column].Field;
  if Field is TDateTimeField then
  begin
    // Formatieren der Zelle als Datum im Format DD.MM.JJ
    AFormatting.Format := 'dd.MM.yy';
    AFormatting.&Type := gdftDate;
    AConvertSettings.ShortDateFormat := 'dd.MM.yy';
    AConvertSettings.DateSeparator := '.';
    ACanFormat := True;
  end;
end;

But it shows timeinformation in some rows.
grafik

ConvertSettings should not be required because you already convert the field to data via AsDateTime. We have however tested this here and couldn't see an issue. What you can also try is to remove the time part from field to data via

ACellValue := Int(AField.AsDateTime);

Yes, it is helpful to cut off the decimal places that represent the time. Great and simple idea.

1 Like