简体   繁体   English

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

[英]TStringgrid change color of row conditionally in firemonkey

How to paint an entire row based on some value in a particular column? 如何根据特定列中的某些值绘制整个行? I have TStringGrid with four columns: 我有四列的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) 

If my column state has value true repaint whole row to be yellow. 如果我的列状态具有值true,则将整个行重绘为黄色。

I have tried this but it only works on column 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;

Just a simple adjustment 只需简单的调整

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;

The better to use a const value instead of '3' in case the column changes. 如果列发生更改,最好使用const值而不是“ 3”。 The point is that this routine will be called for all cells, but you only ever want to compare the value of the 4th column, regardless of which cell is being currently drawn 关键是,将为所有单元格调用此例程,但是您只想比较第4列的值,而不管当前正在绘制哪个单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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