RT:delphi+cxgrid,連接sql顯示出數據,如何根據cxgrid單元格數據自繪顔色???
如:(當單元格的值等於1時,此單元格是紅色;2時是綠色;3時是黃色...)?

解决方案 »

  1.   

    這個比較簡單:在Tableview中的CustomDrawCell事件中寫代碼即可:procedure TRes_HrYd_Approve_F.cxGrid1DBBandedTableView1CustomDrawCell(
      Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    var
      CheckValue:Integer;
      aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk:string;
      CxColumn,aa1,bb1,cc1,dd1,ee1,ff1,gg1,hh1,ii1,jj1,kk1:TcxGridDBBandedColumn;
    begin
      inherited; //程序自繪
      CxColumn:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('FLAG');
      if CxColumn=nil then Exit;
      if SameText(AViewInfo.Item.Name,CxColumn.Name) then
      begin
        CheckValue:=StrToInt(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column28.Index]);
        if(CheckValue=0) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=clRed;
        end;
        if(CheckValue=1) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=clGreen;
        end;
        if(CheckValue=2) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=clFuchsia;
        end;
      end; 
      aa1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XCB');
      if aa1=nil then Exit;
      if SameText(AViewInfo.Item.Name,aa1.Name) then
      begin
        aa:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column27.Index]);
        if (Trim(aa)) <> (Trim(qry_approveyd.FieldByName('YCB').AsString)) then
        begin
          AViewInfo.Focused;                                           
          ACanvas.Brush.Color:=$00B871FF;
        end;
      end;
      bb1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XBM');
      if bb1=nil then Exit;
      if SameText(AViewInfo.Item.Name,bb1.Name) then
      begin
        bb:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column1.Index]);
        if (Trim(bb)) <> (Trim(qry_approveyd.FieldByName('YBM').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;
      end;
      cc1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XKB');
      if cc1=nil then Exit;
      if SameText(AViewInfo.Item.Name,cc1.Name) then
      begin
        cc:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column2.Index]);
        if (Trim(cc)) <> (Trim(qry_approveyd.FieldByName('YKB').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;
      end;
      dd1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XZXB');
      if dd1=nil then Exit;
      if SameText(AViewInfo.Item.Name,dd1.Name) then
      begin
        dd:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column3.Index]);
        if (Trim(dd)) <> (Trim(qry_approveyd.FieldByName('YZXB').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;
      ee1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XBMID');
      if ee1=nil then Exit;
      if SameText(AViewInfo.Item.Name,ee1.Name) then
      begin
        ee:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column13.Index]);
        if (Trim(ee)) <> (Trim(qry_approveyd.FieldByName('YBMID').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;
      ff1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XBC');
      if ff1=nil then Exit;
      if SameText(AViewInfo.Item.Name,ff1.Name) then
      begin
        ff:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column14.Index]);
        if (Trim(ff)) <> (Trim(qry_approveyd.FieldByName('YBC').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;
      kk1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XJZ');
      if kk1=nil then Exit;
      if SameText(AViewInfo.Item.Name,kk1.Name) then
      begin
        kk:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column15.Index]);
        if (Trim(kk)) <> (Trim(qry_approveyd.FieldByName('YJZ').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;  
      gg1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XZJQF');
      if gg1=nil then Exit;
      if SameText(AViewInfo.Item.Name,gg1.Name) then
      begin
        gg:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column16.Index]);
        if (Trim(gg)) <> (Trim(qry_approveyd.FieldByName('YZJQF').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;      
      hh1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XCBZX');
      if hh1=nil then Exit;
      if SameText(AViewInfo.Item.Name,hh1.Name) then
      begin
        hh:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column17.Index]);
        if (Trim(hh)) <> (Trim(qry_approveyd.FieldByName('YCBZX').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;
      ii1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XGW');
      if ii1=nil then Exit;
      if SameText(AViewInfo.Item.Name,ii1.Name) then
      begin
        ii:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column18.Index]);
        if (Trim(ii)) <> (Trim(qry_approveyd.FieldByName('YGW').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;  
      end;
      jj1:=(Sender as TcxGridDBBandedTableView).GetColumnByFieldName('XZW');
      if jj1=nil then Exit;
      if SameText(AViewInfo.Item.Name,jj1.Name) then
      begin
        jj:=VarToStr(AViewInfo.GridRecord.Values[cxGrid1DBBandedTableView1Column19.Index]);
        if (Trim(jj)) <> (Trim(qry_approveyd.FieldByName('YZW').AsString)) then
        begin
          AViewInfo.Focused;
          ACanvas.Brush.Color:=$00B871FF;
        end;
      end;  
    end;
      

  2.   

    CustomDrawCell里自绘,参考楼上的代码。