2

I have a problem in Delphi 10.1 in a multi-device application (on Windows). I have a StringGrid (connected to a db) and I can change background color of row, but the problem is there is a "padding" (in grey/silver) between cells.

In onformCreate I define:

stringgrid1.DefaultDrawing := False;

This is my code:

procedure Tlist_form.StringGrid1DrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
   var aRowColor: TBrush;
begin
  aRowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha);


  if (stringgrid1.Cells[7,row]='1') then 
        aRowColor.Color := TAlphaColors.Green
    else
      aRowColor.Color := TAlphaColors.Red;

    Canvas.FillRect(Bounds, 0, 0, [], 1, aRowColor);
    Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);

  aRowColor.free;

end; 

In Delphi 6 I've never had this problem, and I don't know how to fix-it. Thank you.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
Rube
  • 105
  • 3
  • 11

1 Answers1

6

Solution 1 (at design time):

For each StringColumn locate the Padding property and change all values from 3 to 0.

Solution 2 (at run time):

Add a TRectF to local vars. Assign it the value of Bounds and Inlfate() it. The modified OnDrawColumnCell() looks like this:

procedure TForm30.StringGrid1DrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  aRowColor: TBrush;
  aNewRectF: TRectF;
begin
  aRowColor := TBrush.Create(TBrushKind.Solid, TAlphaColors.Alpha);

  if (StringGrid1.Cells[7, Row] = '1') then
    aRowColor.Color := TAlphaColors.Green
  else
    aRowColor.Color := TAlphaColors.Red;

  aNewRectF := Bounds;
  aNewRectF.Inflate(3, 3);
  Canvas.FillRect(aNewRectF, 0, 0, [], 1, aRowColor);
  Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);

  aRowColor.free;
end;

The grid looks like this with both solutions:

enter image description here

To also remove the lines between the cells, untick ColLines and RowLines in the grids Options.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • it says " E2033 Types of actual and formal var parameters must be identical" on " InflateRect(aNewRectF, 3, 3);" – Rube Jan 25 '17 at 17:09
  • Hmmm... It compiled fine for me. Doublecheck that you declared `aNewRectF` as a `TRectF` (ending in 'F') and also try writing the constants specifically as floats, as 3.0 – Tom Brunberg Jan 25 '17 at 17:29
  • yep are aNewRectF and TRectF, tried changing value, using float, using const but i have alway same error (and cursor goes just after first parameter of InflateRect(aNewRectF,3,3) – Rube Jan 25 '17 at 17:34
  • When you place the mouse cursor on `InflateRect` what does the hintwindow show as the fully qualified name and parameters – Tom Brunberg Jan 25 '17 at 17:39
  • parameters are TRect@,integer,integer – Rube Jan 25 '17 at 17:44
  • I asked what the fully qualified name is. For me it shows `Types.InflateRect()` and additionally "declared in System.Types" For me the parameters are `TRectF@, Single, Single` – Tom Brunberg Jan 25 '17 at 17:56
  • The only way I can reprodce the prolem you claim is by declaring erroneously `aNewRectF: TRect;` as the variable. The correct declaration is `aNewRectF: TRectF;` – Tom Brunberg Jan 25 '17 at 18:12
  • for me it was `windows.InflateRect()`, then i changed line in `system.Types.InflateRect(aNewRectF, 3,3);` and now it works! Thankyou Tom. – Rube Jan 26 '17 at 08:22
  • Another option was to use: aNewRectF.Inflate(3, 3); – roumen Mar 18 '18 at 11:27
  • Yes @roumen that is correct! I chaged my answer accordingly, thanks for the suggestion. – Tom Brunberg Mar 18 '18 at 12:49