CxGrid 改变某行或单元格的颜色 .

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值