Q&A

  • DBGrid에 CheckBox를 넣을수 있나요?
DBgrid의 내용을 뿌리고 맞으면 CheckBox를 체크하도록 하고 싶은데
가능한가요?
2  COMMENTS
  • Profile
    홍성락 2002.09.07 01:34
    hsr/////////////////////////////////////////////////////////
    실행파일은 자료실에 넣어 놓았습니다.
    참조해보세요.
    unit Unit1;

    interface

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

    type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        Database1: TDatabase;
        Table1: TTable;
        DataSource1: TDataSource;
        procedure CheckBoxMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormShow(Sender: TObject);
        procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
        procedure DBGrid1_2DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);

      private
        { Private declarations }
        procedure StringGridInCheckBoxCreate(sg :TStringGrid; Col, Row :Integer; pChecked :Boolean);
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}
    /////////////////////////////////////////////////////////////////////////////
    procedure TForm1.StringGridInCheckBoxCreate(sg :TStringGrid; Col, Row :Integer; pChecked :Boolean);
    var
      CheckBox : TCheckBox;
    begin
      if FindComponent('CheckBox'+intToStr(Col)+ 'V' +intToStr(Row)) <> nil then exit;
      CheckBox := TCheckBox.Create(self);
      with CheckBox do
      begin
        Name       := 'CheckBox'+intToStr(Col)+ 'V' +intToStr(Row);
        OnMouseDown  := CheckBoxMouseDown;
        Parent     := sg;
        Checked    := pChecked;
        BoundsRect := sg.CellRect(Col, Row);
        Width      := 14;
        Height     := sg.RowHeights[Row];
        TabStop    := True;
      end;
    end;
    {==================================
      CheckBoxMouseUp Event Process
    ==================================}
    procedure TForm1.CheckBoxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
       screen.Cursor :=crSQLWait;
       TCheckBox(Sender).Checked := not(TCheckBox(Sender).Checked);
       DBGrid1.OnDrawColumnCell := DBGrid1_2DrawColumnCell;
       DBGrid1.Refresh;
       DBGrid1.OnDrawColumnCell := DBGrid1DrawColumnCell;
       screen.Cursor :=crDefault;
    end;
    /////////////////////////////////////////////////////////////////////////////

    procedure TForm1.FormShow(Sender: TObject);
    var
        i : integer;
    begin
       //DB연결
       Database1.Params.Append('PATH='+ExtractFilePath(Application.ExeName));
       Database1.Params.Append('DEFAULT DRIVER=PARADOX');
       Database1.Params.Append('ENABLE BCD=FALSE');
       Table1.Active := True;

        for i := 1 to TStringGrid(DBGrid1).RowCount-1 do begin
            StringGridInCheckBoxCreate(TStringGrid(DBGrid1),1,i,True);
        end
    end;
    /////////////////////////////////////////////////////////////////////////////

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      Row :Integer;
    begin
      if DataCol = 0 then begin
         Row := DBGrid1.MouseCoord(Rect.Left, Rect.Top).Y;
         if Column.Field.AsInteger = 0 then
            TCheckBox(FindComponent('CheckBox'+intToStr(1)+'V'+intToStr(Row))).Checked :=False
         else
            TCheckBox(FindComponent('CheckBox'+intToStr(1)+'V'+intToStr(Row))).Checked :=True;
      end;
    end;
    procedure TForm1.DBGrid1_2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      Row :Integer;
    begin
      if DataCol = 0 then begin
         Row := DBGrid1.MouseCoord(Rect.Left, Rect.Top).Y;
         if (TCheckBox(FindComponent('CheckBox'+intToStr(1)+'V'+intToStr(Row))).Checked =True)and
            (Column.Field.AsInteger = 0)then begin
            TDBGrid(Sender).DataSource.DataSet.Edit;
            Column.Field.AsInteger := 1;
         end;
         if (TCheckBox(FindComponent('CheckBox'+intToStr(1)+'V'+intToStr(Row))).Checked =False)and
            (Column.Field.AsInteger = 1)then begin
            TDBGrid(Sender).DataSource.DataSet.Edit;
            Column.Field.AsInteger := 0;
         end;
      end;
    end;
    ///////////////////////////////////////////////////////////////////////////////
    end.
  • Profile
    김영남 2002.09.06 00:46
    가능합니다...

    이곳 질답란에 검색해 보시면 많이 보실 수 있을겁니다...

    CheckBox뿐만 아니고 다른 컴포넌트도 가능하구요...

    그렇지만 정확하겐 넣는다기보단 올려 놓는 개념이라고 봐야 하나? ^^