Fmx TStringGrid row color

℡╲_俬逩灬. 提交于 2020-01-04 05:49:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41854676/fmx-tstringgrid-row-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!