HOW TO 实现DBGrid奇数行背景为红色,偶数行为白?

解决方案 »

  1.   

    自己画,dbgrid.ownerdraw:=true;
    在OnDrawColumnCell事件里自定义dbgrid.canvas得颜色,然后调用 DefaultDrawColumnCell方法画
      

  2.   

    procedure TfrmppManager.dbgMMDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      if dbgmm.DataSource.DataSet.RecNo mod 2 = 0 then //条件判断
      begin
      with dbgmm do
        begin
        Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clRed;
        end;
      end;
      dbgmm.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;
      

  3.   

    procedure TForm_Main.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      with DBGrid1.Canvas do
      begin
        if DBGrid1.DataSource.DataSet.RecNo mod 2 =0 then
          DBGrid1.Canvas.Brush.Color:= clBackground
        else
          DBGrid1.Canvas.Brush.Color:= clWhite;
      end;
       DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;