根据数据的值在firemonkey stringgrid上着色单元格背景

时间:2012-10-15 04:46:22

标签: delphi firemonkey

它可能是基本的,但我有一个 * *的时间查找示例代码,以根据Firemonkey中的数据库中的值更改stringgrid的行颜色。我有来自MDB的数据没有问题,但需要行是某些颜色,即'1'=红色'2'=绿色等我知道我必须以某种方式访问​​Style元素'OnApplyStyleLookup'?但在什么阶段。我已经看到了关于改变文本样式和颜色等的问题,但我正在为自己试图找到'背景'元素和应用挖洞。任何帮助将不胜感激。 干杯 理查德......(新手到Firemonkey)

2 个答案:

答案 0 :(得分:4)

{OnDrawColumnCell event}

procedure OnDrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  RowColor : TBrush;
begin

  RowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha);

{you can check for values and then set the color you want}
  if Value.ToString = 'red' then
     RowColor.Color := TAlphaColors.Red;

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

  { perform default drawing }
  TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row,
    Value, State);
end;

答案 1 :(得分:0)

这是我在德尔福柏林的代码工作正常:

var
  aRowColor: TBrush;
begin
  //it's better to write this line into create
  aRowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha);
  //-----
  grid.DefaultDrawing := False;
  if (myTbl.RcrdDataCount > 0) and (Row < myTbl.RcrdDataCount) then begin
    if myTbl.RcrdDataItems[Row].State = TStateDeleted then begin
      aRowColor.Color := TAlphaColors.Red;
    end
    else begin
      aRowColor.Color := TAlphaColors.Gray;
    end;
    Canvas.FillRect(Bounds, 0, 0, [], 1, aRowColor);
    Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);
  end;
  //it's better to write this line into destroy
  aRowColor.free;
  //-----
end;