根据条件自定义 cxGrid 的单元格样式

当指定的单元格需要指定样式(如字体颜色设置为红色,背景色设置为黄色)时,可按如下步骤进行:

1、添加 csStyleRepository 控件,并新建 Style,设置前景(TextColor)、背景色(Color)。

2、编写 cxGrid 中 Table 组件的 Styles.OnGetContentStyle 事件:

procedure TAccumulationFundForm.gridAccumulationFundStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
begin
  if AItem.Index = gridAccumulationFundPERSON_NAME.Index then
  begin
    if ARecord.Values[gridAccumulationFundHAS_EMPLOYEE.Index] = 'N' then
    begin
      AStyle := dmResource.Style_IdCardNotExists;
    end;
  end;
end;

注意:

上面代码的意思是指:如果 AItem 就是要设置 Style 的单元格的话,则找到 ARecord 数据中的指定字段值进行条件匹配,如果成功,则设置 AStyle 为指定的样式对象。这样,则达到效果。

原文地址:https://www.cnblogs.com/xiefang2008/p/4725875.html