cxGrid 行变颜色

来源:互联网 发布:有没有优步抢单软件 编辑:程序博客网 时间:2024/04/28 17:19

界面上有一个按钮,点击后cxgrid显示不同数据,当点击按钮取到的记录在另外一张表中存在时则字体显示为红色
该怎么写,加个标识位,在cxgrid表上
然后在style中的ongetcontentstyle事件中写处理代码,如下面这样
  if ARecord is TcxGridDataRow then
  begin
    if ARecord.Values[2] = 'Y' then
      AStyle := DMImpl.cxStyle18;
  end;

以上代码,点击按钮改变数据后不是马上改变字体 必须要在该行上点击一下才能改变过来

 

procedure TfrmAtt_Operation.grdDataDBTableView1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if vartostr(ARecord.Values[26])='1' then
    AStyle:=frmDM.cxStyle1;
end;

 

 

---------------------------------------------------------------------------------------------------------------

自画,写在cxGridDBTableView的OnCustomDrawCell事件中:     if   (cxGrid1DBTableView1.DataController.DataSet   =   nil)   or   (not   cxGrid1DBTableView1.DataController.DataSet.Active)   then   exit     if   (cxGrid1DBTableView1.DataController.DataSet.RecNo   mod   2)   =   0   then         ACanvas.Font.Color:=RGB(255,0,255);     else         ACanvas.Font.Color:=RGB(255,0,0);=======================================cxGrid 单元格颜色特效2007年02月03日 星期六 09:48一个表(T)的结构结构如下. ID Test 1 2001 2 1444 3 1788 5 2645 6 4568 cxGrid成功连接到该表, 如果要实现单元格特效, 就要在cxGridDBTableView的 OnCustomDrawCell 写代码. 该事件声明原形为 type TcxGridTableDataCellCustomDrawEvent = procedure(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean) of object; 参数 Sender: 你要实现特效的TableView; ACanvas: 画布, 这个参数比较重要, 就是用这个参数画出 特效; AViewInfo: 自定义条件的来源; 从这个参数中获取单元格值; ADone: 设为真就不会Paint. 下面是以 Test字段的值来控件单元格颜色 var CheckValue: integer; cxColumn: TcxGridColumn; begin cxColumn := (Sender as TcxGridDBTableView).GetColumnByFieldName('Test'); if cxColumn = nil then Exit; //这个条件用来限制是否只Paint指定的单元格, 去掉则Paint整行. if SameText(AViewInfo.Item.Name, cxColumn.Name) then begin CheckValue := AViewInfo.GridRecord.Values[gdtvTestTest.Index]; //获取单元格 //以下是满足条件的字体变色 if CheckValue >= 4000 then //大于4000为红色 ACanvas.Font.Color := clRed else if CheckValue >= 3000 then //大于3000绿色 ACanvas.Font.Color := clGreen else if CheckValue >= 2000 then //大于2000蓝色 ACanvas.Font.Color := clBlue; //以下是满足条件的数据背景变色 {if CheckValue >= 4000 then begin //大于4000为红色 AViewInfo.Focused; ACanvas.Brush.Color := clRed end else if CheckValue >= 3000 then //大于3000绿色 ACanvas.Brush.Color := clGreen else if CheckValue >= 2000 then //大于2000蓝色 ACanvas.Brush.Color := clBlue; } end; end; ------------------------------------------------------------------------------------------

在cxGridDBTableView的OnCustomDrawCell事件中

 

设置行的颜色

var

   ARec: TRect;

begin

   ARec := AViewInfo.Bounds;

   ACanvas.canvas.brush.color:= clGreen;

   ACanvas.FillRect(ARec)

end;

 

设置单元格的颜色

var

   ARec: TRect;

begin

   ARec := AViewInfo.ClientBounds;

   ACanvas.canvas.brush.color:= clGreen;

   ACanvas.FillRect(ARec)

end;

==========================================================procedure TFm_Rkcx.GD_RkcxBTCustomDrawCell(Sender: TcxCustomGridTableView;  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;  var ADone: Boolean);var ATextToDraw:String;    ARec: TRect;begin  ATextToDraw := AViewInfo.GridRecord.DisplayTexts[AViewInfo.Item.Index];  ARec := AViewInfo.Bounds;  if AViewInfo.GridRecord.Values[6]<1000 then    ACanvas.Canvas.Font.Color := clRed;  //整行变色:ACanvas.Canvas.brush.color:=clred;    {列的颜色交替    if AViewInfo.Item.Index mod 2 = 0 then      ACanvas.Canvas.brush.color := clInfoBk    else    ACanvas.Canvas.brush.color := clInactiveCaptionText;  }  {行的颜色交替     if AViewInfo.RecordViewInfo.Index mod 2 = 0 then    ACanvas.Canvas.brush.color := clInfoBk  else    ACanvas.Canvas.brush.color := clInactiveCaptionText;   }   ACanvas.Canvas.FillRect(ARec);end;如果第一个字段的值小于125,用红色背景显示,文字为居中显示procedure TForm1.cxGrid1DBTableView1CustomDrawCell( Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);begin     if StrToInt(AViewInfo.GridRecord.DisplayTexts[0])<125 then        begin           ACanvas.Brush.Color:=clRed;           ACanvas.FillRect(AViewInfo.Bounds);           ACanvas.DrawText(AViewInfo.GridRecord.DisplayTexts[AViewInfo.Item.Index], AViewInfo.Bounds, cxAlignHCenter);           ADone:=true;        end;end;  ===================================================

如何使满足条件的数据显示不同的颜色?  
  解决:  
  var  
  AYellowStyle:   TcxStyle;  
   
  procedure   TForm1.FormCreate(Sender:   TObject);  
  begin  
    //行颜色  
    AYellowStyle   :=   TcxStyle.Create(Self);  
    AYellowStyle.Color   :=   $0080FFFF;  
    AYellowStyle.TextColor   :=   clMaroon;  
  end;  
   
  procedure   TForm1.cxGrid1DBBandedTableView1StylesGetContentStyle(  
  Sender:   TcxCustomGridTableView;   ARecord:   TcxCustomGridRecord;  
  AItem:   TcxCustomGridTableItem;   out   AStyle:   TcxStyle);  
  begin  
     //  这里cxGrid1DBBandedTableView1Lengthcm.Index小于81时就显示黄色  

     if   ARecord.Values[cxGrid1DBBandedTableView1Lengthcm.Index]   <   81   then    

        AStyle   :=   AYellowStyle;  

     //或者 ARecord.Values[8]   :cxgrid中第8列的值

     if   vartostr(ARecord.Values[8])   ='已审'   then  
        AStyle   :=   AYellowStyle;   

  end;  

 

===========================================================

 

CXGrid 控件整行变颜色

  CXGrid 控件整行变颜色当某行符合条件的时候,给它变色:           if not VarIsNull(AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('Status').Index]) then    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('Status').Index] = '选中' then    begin      ACanvas.Canvas.Font.Color := clBlack;     
ACanvas.Brush.Color := clRed;   
end;
原创粉丝点击