简体   繁体   中英

Fmx TStringGrid row color

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.

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM