How do I color a cxgrid based on table value?

孤人 提交于 2019-12-12 11:35:41

问题


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 ?


回答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




回答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.




回答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;



回答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;



回答5:


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.



来源:https://stackoverflow.com/questions/6078715/how-do-i-color-a-cxgrid-based-on-table-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!