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.
실행파일은 자료실에 넣어 놓았습니다.
참조해보세요.
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.