TStringgrid 在 firemonkey 中有条件地改变行的颜色

TStringgrid change color of row conditionally in firemonkey

如何根据特定列中的某个值绘制整行? 我有四列的 TStringGrid:

 ID  | NAME   |   DATE      |  STATE
 1     X       2017-01-01      TRUE          --whole row need to be yellow
 2     Y       2017-01-01      FALSE         --default color (no change) 

如果我的列 state 的值为 true,则将整行重绘为黄色。

我试过了,但它只适用于 state 列:

procedure TTNarudzbenice.grSomeNameDrawColumnCell(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 Value.ToString = 'TRUE' then
    begin
    aRowColor.Color := TAlphaColors.Yellow;

    Canvas.FillRect(Bounds, 0, 0, [], 1, aRowColor);

    Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);

end;

    aRowColor.free;

end;

只是一个简单的调整

procedure TTNarudzbenice.grSomeNameDrawColumnCell(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 (Sender as TStringGrid).Cells[ 3, Row ] = 'TRUE' then  //// This line changed
  begin
    aRowColor.Color := TAlphaColors.Yellow;

    Canvas.FillRect(Bounds, 0, 0, [], 1, aRowColor);

    Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);

  end;
  aRowColor.free;

end;

最好使用 const 值而不是“3”,以防列发生变化。关键是这个例程将被所有单元格调用,但你只想比较第 4 列的值,而不管当前正在绘制哪个单元格