1、

【转】CxGrid 改变某行或单元格的颜色

 (2016-01-19 09:37:19)
标签: 

it

 

delphi

分类: Delphi

一个表(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;

 

以上内容为转贴,在使用过程中,发现作者固定了字段Test,而我遇到的问题是:客户提出对cxGrid中不可编辑的列用特别的颜色标记,这时候,字段就不是固定的,于是对上面的代码做了改造:

 

procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  cxColumn: TcxGridColumn;
  aFieldName:String;
begin
  aFieldName:=TcxGridItemDBDataBinding(AViewInfo.Item.DataBinding).FieldName;

 //引用 cxGridDBDataDefinitions单元,才支持TcxGridItemDBDataBinding类.这行取出当前列的字段名
  cxColumn := (Sender as TcxGridDBTableView).GetColumnByFieldName(aFieldName);//按字段名取出Column对象
    if not cxColumn.Options.Focusing then//不允许编辑的列标记为特殊颜色.
      ACanvas.Brush.Color:=clRed;
end;

 

上面的代码,实现当cxGrid中某一列.Options.Focusing:=False时,用红色显示.

 
 
 
2、这是第二个方式
  1. procedure TForm1.cxGrid1DBTableView1CustomDrawCell(  
  2.   Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;  
  3.   AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);  
  4. begin  
  5.   //Item确定列(ID列的索引),RecordViewInfo确定行(Index行的索引)  
  6.   //可以定位到某一行改变颜色  
  7.  {if (AViewInfo.Item.ID = 0) and (AViewInfo.RecordViewInfo.Index = 0) then  
  8.     ACanvas.Brush.Color := clred;}  
  9.   //可以根据条件值确定改变某行颜色  
  10.  if (trim(AViewInfo.RecordViewInfo.GridRecord.Values[4]) = 'HTT')   
  11.     and (AViewInfo.Item.ID = 4) //确定到某一列,如果不加确定是某行底色  
  12.     then  
  13.     ACanvas.Brush.Color := clred;  
  14. end;  

 

3、看这个贴子 

cxgrid几种条件下行变色问题与解决
1 .鼠标选中行变色。
其实默认条件下,鼠标选中的行就会变色,但是被点中的单元格不变色,如果想让单元格和其它列一样的话,在cxGrid1DBTableView中找属性OptionsSelection中的CellSelect的属性变成False.但是默认颜色可能达不到使用者的要求,比如有粉粉控的人只喜欢粉色,想让选中的行变成粉色怎么办呢?很简单,只需要使cxStyleRepository控件在styles中加一种颜色就可以了。效果如图


2 .焦点不再cxgrid表中但数据库中数据对应的行变颜色。
方法有很多,我只说一种,原理是利用数据库中所指的当前行数据,然后对应到cxgrid表上。

procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if iewinfo.RecordViewInfo.Index=cxGrid1DBTableView1.Da taController.GetFocusedRowIndex then
acanvas.Brush.Color:=clgreen;
end;
1
2
3
4
5
6
7
Item确定列(ID列的索引),RecordViewInfo确定行(Index行的索引)
可以定位到某一行改变颜色
if (AViewInfo.Item.ID = 0) //0列
if (AViewInfo.RecordViewInfo.Index = 0) //0行

3 .固定某行按条件变色
procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('SheBeiBianHao').Index])='20050419' then

begin
ACanvas.Brush.Color := clRed;
end;
---------------------
作者:singular2611
来源:CSDN
原文:https://blog.csdn.net/singular2611/article/details/44725359
版权声明:本文为博主原创文章,转载请附上博文链接!