9

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

4

2 回答 2

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;
于 2014-08-22T17:08:11.520 回答
0

这是我在 Delphi Berlin 的代码,效果很好:

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;
于 2016-10-06T10:12:32.623 回答