【问题标题】:TStringgrid change color of row conditionally in firemonkeyTStringgrid在firemonkey中有条件地改变行的颜色
【发布时间】:2017-01-17 11:28:00
【问题描述】:

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

【问题讨论】:

    标签: delphi firemonkey tstringgrid


    【解决方案1】:

    只是一个简单的调整

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

    【讨论】:

    • 太棒了,我也把 Padding 改成了 0。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2020-12-12
    相关资源
    最近更新 更多