Coloring cell background on firemonkey stringgrid according to value from data

大城市里の小女人 提交于 2019-12-22 04:08:09

问题


It may be basic but I'm have a ** of a time finding sample code to change a row color of a stringgrid based on a value from a database in Firemonkey. I have data coming from a MDB no problems but need the row to be certain colors for ie '1' = red '2' = green etc. I know I have to access the Style elements somehow 'OnApplyStyleLookup'? but at what stage. I have seen questions on changing text style and colour etc but I am digging a hole for myself trying to get to the 'background' element and applying. Any help would be greatly appreciated. Cheers Richard ...(newbie to Firemonkey)


回答1:


{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;



回答2:


This is my code with Delphi Berlin which works fine:

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;


来源:https://stackoverflow.com/questions/12889428/coloring-cell-background-on-firemonkey-stringgrid-according-to-value-from-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!