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

时间:2017-01-17 11:28:00

标签: delphi firemonkey tstringgrid

如何根据特定列中的某些值绘制整行? 我有四列的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) 

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

我试过这个但是它只适用于状态

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;

1 个答案:

答案 0 :(得分:3)

只是一个简单的调整

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列的值,而不管当前正在绘制哪个单元格