Search code examples
delphidevexpresstcxgrid

How do I color a cxgrid based on table value?


I would like all rows where in particular field name 'hello' is present to get colored green. I tried this on customdrawcell:

if abstable1.fieldbyname('somename').asstring = 'Hello' then
  cxgrid.canvas.brush.color:=clGreen

But it wont work... what am I missing here ?


Solution

  • Don't try to change canvas colors in the Grid. Rather--and I find this to always be true--change colors in the View's OnDrawCell handler, as in this example:

    procedure T_fmTabSnapList.View1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    begin
      if abstable1.fieldbyname('somename').asstring = 'Hello' then 
        ACanvas.Brush.Color := clGreen
    end;
    

    The cxGrid is just a container for Views. Views are where all the painting occurs. s