How do I color a cxgrid based on table value?

33,591

Solution 1

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

Solution 2

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

Solution 3

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;

Solution 4

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;
Share:
33,591
user763539
Author by

user763539

Updated on May 29, 2020

Comments

  • user763539
    user763539 almost 4 years

    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 ?

  • user763539
    user763539 almost 13 years
    Got it to work ....if(AViewInfo.GridRecord.Values[DataModule2.ABSTable7.Ind‌​ex]...Thank you all...
  • avenmore
    avenmore almost 13 years
    @user763539: will work, as long as you have view columns for every field in the table and they are in the same order.
  • Rafael Piccolo
    Rafael Piccolo about 12 years
    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.