Fmx TStringGrid行颜色

时间:2017-01-25 14:55:12

标签: delphi background-color firemonkey delphi-10.1-berlin stringgrid

我在多设备应用程序(在Windows上)的Delphi 10.1中遇到了问题。 我有StringGrid(连接到数据库),我可以更改行的背景颜色,但问题是有一个"填充" (细胞之间的灰色/银色)。

onformCreate我定义:

stringgrid1.DefaultDrawing := False;

这是我的代码:

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; 

在Delphi 6中,我从未遇到过这个问题,而且我也不知道如何修复它。 谢谢。

1 个答案:

答案 0 :(得分:3)

解决方案1(在设计时):

为每个StringColumn找到Padding属性,并将所有值从3更改为0.

解决方案2(在运行时):

向本地变量添加TRectF。为其指定BoundsInlfate()的值。修改后的OnDrawColumnCell()如下所示:

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;

两种解决方案都是这样的网格:

enter image description here

要同时移除单元格之间的线条,请在网格ColLines中取消选中RowLinesOptions

相关问题