4

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 ?

RRUZ
  • 134,889
  • 20
  • 356
  • 483
user763539
  • 3,509
  • 6
  • 44
  • 103
  • Post your actual code for the entire event handler, not something you just made up for your question. Also, format it by either indenting it by four spaces or selecting it all and clicking the {} button or pressing Ctrl+K. Actual code allows us to see what's wrong, or at least copy and paste to help figure it out. – Ken White May 21 '11 at 01:02

5 Answers5

9

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

Sam M
  • 4,136
  • 4
  • 29
  • 42
  • 2
    Yes, 100%. Styles are much easier to work and safer too. By separating the logic that chooses the style from how each style should be presented, you are free to change one of them without compromising the other (like HTML and CSS). Besides that, you also promotes reusability and standardization, since the styles can be shared among different grids. – Rafael Piccolo Apr 19 '12 at 12:03
6

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;
avenmore
  • 2,809
  • 3
  • 33
  • 34
  • @user763539: will work, as long as you have view columns for every field in the table and they are in the same order. – avenmore May 27 '11 at 17:35
5

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

1
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;
Akella225
  • 61
  • 2
  • 8
0

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.

No'am Newman
  • 6,395
  • 5
  • 38
  • 50