Q&A

  • DBGrid에서 홀수 행과 짝수행의 색을 다르게 하려고 합니다.
제목 처럼 DBgrid에 내용을 출력을 할 때
1행은 흰색
2행은 검은색
3행은 흰색
4행은 검은색
5행은 흰색
......
이런식으로 출력하려고 합니다.
어떻게 해야 할지 좀 ...
2  COMMENTS
  • Profile
    신석기 2002.07.23 09:44
    이런방법도 있습니다. 간단히 ^^
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    var
       RH,R : Integer;
    begin
       RH := Rect.Bottom - Rect.Top;
       R  := (Rect.top div RH) - 1;
       R  := R mod 2;
       if (R = 0) then begin
           (Sender as TDbGrid).canvas.brush.Color := clInfoBk;
           (Sender as TDbGrid).canvas.font.Color  := clWindowText;
           (Sender as TDbGrid).canvas.FillRect(Rect);
           (sender as TDbGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
       end
       else begin
           (sender as TDbGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
       end ;
    end;
  • Profile
    홍성락 2002.07.23 09:16
    hsr////////////////////////////////////////////////////////////////
    좀 어색하지만.....
    아래처럼해보세요.

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, DBTables, Grids, DBGrids;

    type
      TForm1 = class(TForm)
        DBGrid2: TDBGrid;
        DataSource2: TDataSource;
        Table2: TTable;
        procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        SaveWndProc: TWndMethod;
        procedure WndProc(var Message: TMessage);
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      row : integer;
    begin
       with TDBGrid(Sender) do begin
             row := MouseCoord(Rect.Left, Rect.Top).Y;
             if ( row mod 2)=0 then begin
                Canvas.Brush.Color :=clBlack;
                Canvas.Font.Color:=clWhite;
             end
             else begin
                Canvas.Brush.Color :=clWhite;
                Canvas.Font.Color:=clBlack;
             end;
             DefaultDrawColumnCell(Rect, DataCol, Column,[]);
        end;
    end;

    procedure TForm1.WndProc(var Message: TMessage);
    begin
      if (Message.Msg = WM_HSCROLL) or (Message.Msg = WM_VSCROLL) then
      begin
          DBGrid2.Repaint;
      end;

      SaveWndProc(Message);
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
        SaveWndProc := DBGrid2.WindowProc;
        DBGrid2.WindowProc := WndProc;
    end;

    end.