Cxgrid 单元格 内容 颜色 关键字高…

来源:互联网 发布:python中遍历字符串 编辑:程序博客网 时间:2024/06/07 19:26

cxgrid列表的列一定要手动提前绑定好。后期绑定的,颜色是会变不了的。注意了。因为我的列表全部都是自动从数据库加载的。搞了好几天。手动绑定列表之后。才可以变颜色了。

 

效果图片如下显示:Cxgrid <wbr>单元格 <wbr>内容 <wbr>颜色 <wbr>关键字高亮显示
procedure TXXXXXForm.GridViewCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas:TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; varADone: Boolean);

var
  I,I1,I2,I3,I4: Integer;
  ViewCaption: string;
  ARec:TRect;  
  Column1,Column2,Column3,Column4:TcxGridColumn;
begin
  inherited;
  //变整行颜色
 { with ACanvas do
  begin
    if notVarIsNull(TcxGridDBTableView(Sender).DataController.DataSource.DataSet.FieldByName('C1').Value)then
   if AViewInfo.GridRecord.DisplayTexts[TcxGridDBTableView(Sender).GetColumnByFieldName('C1').Index]= '空闲' then
   begin  //标识负数记录
     Canvas.Brush.Color:= $00669900;
     Canvas.Font.Color:= clWhite;//clActiveCaption
    end;
  }
{颜色分为四种:空闲 $00669900 试用 $00FF99FF  使用$00CA66FF  请假 $0033CCFF }
   I1 := -1;
   I2 := -1;
   I3 := -1;
   I4 := -1;
   with  (Senderas TcxGridDBTableView) do
   begin
    for I := 0 to ColumnCount - 1 do
    begin
      if UpperCase(Columns[I].DataBinding.FieldName)='C1' then
      begin
        I1 := I;
        Column1 := GetColumnByFieldName('C1');
      end
      else if UpperCase(Columns[I].DataBinding.FieldName)='C2' then
      begin
        I2 := I;
        Column2 := GetColumnByFieldName('C2');
      end
      else
      if UpperCase(Columns[I].DataBinding.FieldName)='C3' then
      begin
        I3 := I;
        Column3 := GetColumnByFieldName('C3');
      end
      else if UpperCase(Columns[I].DataBinding.FieldName)='C4' then
      begin
        I4 := I;
        Column4 := GetColumnByFieldName('C4');
      end;
    end;
   end;
   if (Column1 = nil) and(Column2=nil) and (Column3 = nil) and(Column4=nil)  then
    Exit;

   //这个条件用来限制是否只Paint指定的单元格,去掉则Paint整行.
    ifSameText(AViewInfo.Item.Name, Column1.Name) then
  begin             
    if AViewInfo.GridRecord.Values[I1] = null then Exit;
    ViewCaption := AViewInfo.GridRecord.Values[I1]; //获取单元格
    //以下是满足条件的字体变色
     if Pos('空闲',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00669900;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('试用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00FF99FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('使用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00CA66FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('请假',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $0033CCFF;
        ACanvas.Font.Color := clblack ;
     end
     else
     begin
        ACanvas.Font.Color := clWhite;
        ACanvas.Brush.Color := clblack ;
     end;
   end
   else ifSameText(AViewInfo.Item.Name, Column2.Name) then
   begin
    if AViewInfo.GridRecord.Values[I2] = null then Exit;
    ViewCaption := AViewInfo.GridRecord.Values[I2]; //获取单元格
    //以下是满足条件的字体变色
     if Pos('空闲',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00669900;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('试用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00FF99FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('使用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00CA66FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('请假',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $0033CCFF;
        ACanvas.Font.Color := clblack ;
     end
     else
     begin
        ACanvas.Font.Color := clWhite;
        ACanvas.Brush.Color := clblack ;
     end;
   end
   else ifSameText(AViewInfo.Item.Name, Column3.Name) then
   begin
    if AViewInfo.GridRecord.Values[I3] = null then Exit;
    ViewCaption := AViewInfo.GridRecord.Values[I3]; //获取单元格
    //以下是满足条件的字体变色
     if Pos('空闲',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00669900;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('试用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00FF99FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('使用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00CA66FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('请假',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $0033CCFF;
        ACanvas.Font.Color := clBlack;
     end
     else
     begin
        ACanvas.Font.Color := clWhite;
        ACanvas.Brush.Color := clblack ;
     end;
   end
   else ifSameText(AViewInfo.Item.Name, Column4.Name) then
   begin
    if AViewInfo.GridRecord.Values[I4] = null then Exit;
    ViewCaption := AViewInfo.GridRecord.Values[I4]; //获取单元格
    //以下是满足条件的字体变色
     if Pos('空闲',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00669900;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('试用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00FF99FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('使用',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $00CA66FF;
        ACanvas.Font.Color := clWhite ;
     end
     else if Pos('请假',ViewCaption) > 0 then
     begin
        ACanvas.Brush.Color := $0033CCFF;
        ACanvas.Font.Color := clblack ;
     end
     else
     begin
        ACanvas.Font.Color := clWhite;
        ACanvas.Brush.Color := clblack ;
     end;
   end ;
end;

 

效果图片如下显示:Cxgrid <wbr>单元格 <wbr>内容 <wbr>颜色 <wbr>关键字高亮显示

0 0
原创粉丝点击