Question

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 ?

Was it helpful?

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

OTHER TIPS

Use the OnGetContentStyle event for either individual columns or the grid object. Styles are much easier to work with than messing with the canvas.

You need to look at the internal data for each view row rather that the data of the current position in the table. Also make use of the canvas provided in the OnCustomDrawCell() event.

procedure TForm1.YourViewCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if(AViewInfo.GridRecord.Values[YourColumn.Index] = 'Hello') then
    ACanvas.Brush.Color := clGreen;
end;
procedure Tfm1.Grid1StylesGetContentStyle(Sender: TcxCustomGridTableView;
  ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
  out AStyle: TcxStyle);
Var
 Style1: TcxStyle;
begin
 if AItem = nil then exit;

 if ARecord.Values[Grid1Med.Index] = true then
   AStyle := cxStyleMed;

 if ARecord.Values[Grid1CP.Index] = true then
   AStyle := cxStyleHost;

 if (ARecord.Values[Grid1Med.Index] = true) and
    (ARecord.Values[Grid1CP.Index] = true) then
   AStyle := cxStyleHostAndCP;

  if not VarIsNull(ARecord.Values[colColor.Index]) then
  begin
    if not Assigned(AStyle) then
      AStyle := TcxStyle.Create(Sender);
    AStyle.Color := ARecord.Values[colColor.Index];
  end;
end;

Here's some working code from a program of mine which does something similar.

procedure TDoDockets.grDocketsDrawColumnCell(Sender: TObject;
            const Rect: TRect; DataCol: Integer; Column: TColumn;
            State: TGridDrawState);
begin
 with grDockets do
  begin
   if (qDocketsOpenCost.asinteger > 1) and (datacol = 5)
    then canvas.font.color:= clRed;

   if datacol = 9 then // status
    if qDocketsColour.Value <> 0
     then Canvas.font.color:= tcolor (qDocketsColour.Value);

   if datacol = 10 then // attention
    if qDocketsAttention.asinteger = 1
     then canvas.brush.color:= clRed;

   if qDocketsUrgent.asinteger = 1 then canvas.brush.color:= 10092543;
   // light yellow
   DefaultDrawColumnCell (Rect, DataCol, Column, State);
  end
end;

grDockets is the grid, and qDockets is the query being displayed within the grid. Certain columns may be drawn in a different colour depending on the value being displayed, and in one case (qDocketsUrgent = 1), the entire line's background colour is changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top