Manuel Paz
Manuel Paz

Reputation: 39

change the cell color of a StringGrid when entering data in the cell. Delphi

Good evening, I would like to know how to change the color of a cell when writing data in it

I have this...

procedure TFrmReportes.SGDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin

   if (gdSelected in State) then
      begin
      SG.Canvas.Brush.Color := rgb(255,119,119);
      SG.Canvas.FillRect(SG.CellRect(ACol, ARow));
      SG.Canvas.TextOut(Rect.Left+2,Rect.Top+2, SG.Cells[ACol, ARow]);

      end;
end;

but when entering data in the cell, it turns white

Thanks Again!!!

Upvotes: 1

Views: 759

Answers (2)

Manuel Paz
Manuel Paz

Reputation: 39

I found the following code that served me thanks to your suggestion in the previous example with the InplaceEditor property

type
    THackGrid = class(TCustomGrid)
    public
      property InPlaceEditor;
      property EditorMode;
    end;

TFrmReportes = class(TForm)
    SG: TStringGrid;
    ...
  end;

...

procedure TFrmReportes.Button1Click(Sender: TObject);
begin
   THackGrid(SG).InPlaceEditor.Brush.Color := RGB(255, 119, 119);
end;

thank you very much

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 598394

TStringGrid displays a TInplaceEdit on top of the cell currently being edited. That TInplaceEdit covers the entire cell. That is why you don't see your custom drawing. You would need to change the TInplaceEdit's Color property instead. You can access the TInplaceEdit via the TStringGrid.InplaceEditor property.

I would suggest deriving a new component from TStringGrid and override its virtual CreateEditor() method. If there is only 1 grid in your Form, a simple interposer would suffice, eg:

type
  TStringGrid = class(Vcl.Grids.TStringGrid)
  protected
    function CreateEditor: TInplaceEdit; override;
  end;

  TFrmReportes = class(TForm)
    SG: TStringGrid;
    ...
  end;

...

type
  TInplaceEditAccess = class(TInplaceEdit)
  end;

function TStringGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  TInplaceEditAccess(Result).Color := RGB(255, 119, 119);
end;

Upvotes: 4

Related Questions