Q&A

  • DBGrid안에 dbcheckbox넣는 방법좀 갈켜주세요....
제목 그대로 입니다..

급합니다... 추석데 쉴려면, 오늘 해결해야할 문제중 하나입니다..

도와주십시요..

1  COMMENTS
  • Profile
    김환수 2000.09.09 21:12
    Re: DBGrid안에 dbcheckbox넣는 방법좀 갈켜주세요....



    왕왕왕 초보가 도움이 될때도 있네요



    삼각형 출판사의 Inside secrets Delphi 5의 제3부 제10장 p571~574까지에 자세히 나와있네요



    소스는 아래와 같고요

    혹은 http://www.sybex.com 에서 catalog선택, 2565 입력 후

    Mastering Delphi 5가 나오면 순서대로 해서 sourcecode.exe를 다운 받으면

    앞서 얘긴한 Part3 - 10 - CheckDbg 안에 예가 있습니다.

    ( 직접: http://www.sybex.com/cgi-bin/rd_sup_temp.pl?2565sup.html )



    //////소스

    unit CheckF;



    interface



    uses

    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    Db, DBTables, StdCtrls, Mask, DBCtrls, ExtCtrls, Grids, DBGrids, ComCtrls;



    type

    TDbaForm = class(TForm)

    Table1: TTable;

    DataSource1: TDataSource;

    Table1LastName: TStringField;

    Table1FirstName: TStringField;

    Table1Department: TSmallintField;

    Table1Branch: TStringField;

    Table1Senior: TBooleanField;

    Table1HireDate: TDateField;

    DBGrid1: TDBGrid;

    DBCheckBox1: TDBCheckBox;

    procedure FormCreate(Sender: TObject);

    procedure Table1DepartmentGetText(Sender: TField; var Text: String;

    DisplayText: Boolean);

    procedure Table1DepartmentSetText(Sender: TField; const Text: String);

    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;

    DataCol: Integer; Column: TColumn; State: TGridDrawState);

    procedure DBGrid1ColEnter(Sender: TObject);

    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);

    private

    { Private declarations }

    end;



    var

    DbaForm: TDbaForm;



    implementation



    {$R *.DFM}



    procedure TDbaForm.FormCreate(Sender: TObject);

    begin

    if not Table1.Exists then

    begin

    Table1.CreateTable;

    ShowMessage ('You can add data to this table'#13 +

    'by using the DbAware example of Chapter 9');

    end;

    Table1.Open;

    end;



    procedure TDbaForm.Table1DepartmentGetText(Sender: TField;

    var Text: String; DisplayText: Boolean);

    begin

    case Sender.AsInteger of

    1: Text := 'Sales';

    2: Text := 'Accounting';

    3: Text := 'Production';

    4: Text := 'Management';

    else

    Text := '[Error]';

    end;

    end;



    procedure TDbaForm.Table1DepartmentSetText(Sender: TField;

    const Text: String);

    begin

    if Text = 'Sales' then

    Sender.Value := 1

    else if Text = 'Accounting' then

    Sender.Value := 2

    else if Text = 'Production' then

    Sender.Value := 3

    else if Text = 'Management' then

    Sender.Value := 4

    else

    raise Exception.Create ('Error in Department field conversion');

    end;



    procedure TDbaForm.DBGrid1DrawColumnCell(Sender: TObject;

    const Rect: TRect; DataCol: Integer; Column: TColumn;

    State: TGridDrawState);

    begin

    if (gdFocused in State) and

    (Column.Field = Table1Senior) then

    begin

    DBCheckBox1.SetBounds (

    Rect.Left + DBGrid1.Left + 1,

    Rect.Top + DBGrid1.Top + 1,

    Rect.Right - Rect.Left,

    Rect.Bottom - Rect.Top);

    end;

    end;



    procedure TDbaForm.DBGrid1ColEnter(Sender: TObject);

    begin

    if DBGrid1.Columns [DBGrid1.SelectedIndex].

    Field = Table1Senior then

    DBCheckBox1.Visible := True

    else

    DBCheckBox1.Visible := False;

    end;



    procedure TDbaForm.DBGrid1KeyPress(Sender: TObject; var Key: Char);

    begin

    if DBCheckBox1.Visible and (Ord (Key) > 31) then

    begin

    Key := #0;

    Table1.Edit;

    DBCheckBox1.Checked := not

    DBCheckBox1.Checked;

    DBCheckBox1.Field.AsBoolean :=

    DBCheckBox1.Checked;

    end;

    end;



    end.



    그럼........왕왕왕 초보가 도움이 되었기를