Search code examples
delphicelldbgrid

Delphi DBGrid how to change the value when displayed


I need to change the contents of the cell for display only. I do it in DrawColumnCell:

if Column.FieldName = 'UUID' then
  begin
     with (Sender as TDBGrid).Canvas do
     begin
         FillRect(Rect);
         strTemp := (Sender as TDBGrid).DataSource.DataSet.FieldByName('UUID').AsString;
         strTemp := RightStr(strTemp, Length(strTemp) - LastDelimiter('-', strTemp));

         TextOut(Rect.Right - 2 - (Sender as TDBGrid).Canvas.TextWidth(strTemp), Rect.Top + 2, strTemp);

     end;
  end;

UUID - has an appearance 'SHRYU-5TRP0-RT-898-763523561294'. Оnly need to be displayed '763523561294'.

The code from above copes with this, but if you select this cell, it turns out that the original value and mine and they overlap each other are substituted. How to avoid this?

screenshot


Solution

  • The OnGetText() event for the underlying field of the DataSet is a more appropriate place to do this. Use persistent fields or hook up an event at runtime.

    procedure TForm1.FDMemTable1AGUIDGetText(Sender: TField; var Text: string;
        DisplayText: Boolean);
    Var
      strTemp : string;
    begin
      if DisplayText then
      begin
         strTemp := Sender.AsString;
         strTemp := RightStr(strTemp, Length(strTemp) - LastDelimiter('-', strTemp));
         Text := strTemp;
      end;
    end;