请问如何在dbgrid中让奇数行记录背景显示一种颜色、偶数行记录背景显示另一种颜色呢?最好有代码!谢谢!也就是说隔行背景显示相同颜色

解决方案 »

  1.   

    procedure TWaitSettleFrm.DbgWaitDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      if (AdoQryWait.RecNo and $0001) = 1 then
      begin
        DbgWait.Canvas.Brush.Color := $00DDAAB8; // ur color 
      end
      else
      begin
        DbgWait.Canvas.Brush.Color := $00F7FFFF; // ur color
      end;
      DbgWait.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
      

  2.   

    if (adoquery1.recno mod 2)=0 then
        begin
          with (Sender as TDbGrid).Canvas do
          begin
            Brush.Color:=$00FFFFC1;
            FillRect(Rect);
          end;
        end;
       (Sender as TDbGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
      

  3.   

    能不能带一些注释!谢谢!
    我刚才把上面两位哥哥的写入程序,结果还是不行啊。
    liuqifeiyu(liuqi) 报错如下:
    [Error] Unit1.pas(38): There is no overloaded version of 'Rect' that can be called with these arguments
    [Error] Unit1.pas(41): There is no overloaded version of 'Rect' that can be called with these arguments
    [Error] Unit1.pas(41): Undeclared identifier: 'State'
    [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
      

  4.   

    在DBGrid的OnDrawColumnCell事件中写入下代码:procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      oldcolor: TColor;
      oldpm: TPenmode;
    begin
      if (DBGrid1.DataSource.DataSet.RecNo mod 2)=1  then
      begin
        with (Sender as TDbGrid).Canvas do
        begin
          Brush.Color:=$00FFFFC1;
          FillRect(Rect);
        end;
      end;
      (Sender as TDbGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;