user763539
user763539

Reputation: 3719

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 ?

Upvotes: 4

Views: 36322

Answers (5)

Akella225
Akella225

Reputation: 171

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;

Upvotes: 1

avenmore
avenmore

Reputation: 2885

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;

Upvotes: 6

Sam M
Sam M

Reputation: 4166

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

Upvotes: 9

Mark Wilsdorf
Mark Wilsdorf

Reputation: 74

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

Upvotes: 5

No'am Newman
No'am Newman

Reputation: 6477

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.

Upvotes: 0

Related Questions