Arsip: Membuat Check Box Pada DBGrid


by MrMixer in Articles more 13 years ago 4429
Delphi telah menyediakan VCL DBGRID untuk menampilkan data dalam bentuk tabulasi. Namun sayangnya, DBGRID yang tersedia ini tidak dapat menampilkan Check Box untuk field bertipe boolean (true/false) seperti layaknya grid yang dimiliki pada program Ms Access.
Nah.. mungkin jika anda ingin menampilkan Check Box untuk field boolean seperti Ms Access, coba aja nih ikuti contoh program berikut ini.
Selamat Mencoba...
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1CellClick(Column: TColumn);
procedure DBGrid1ColEnter(Sender: TObject);
procedure DBGrid1ColExit(Sender: TObject);
procedure SaveBoolean;
private
{ Private declarations }
FOriginalOptions : TDBGridOptions;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.SaveBoolean;
begin
DBGrid1.SelectedField.Dataset.Edit;
DBGrid1.SelectedField.AsBoolean := not Self.DBGrid1.SelectedField.AsBoolean;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
Const
CtrlState : array[Boolean] of Integer = (DFCS_BUTTONCHECK,
DFCS_BUTTONCHECK or DFCS_CHECKED);
var
CheckBoxRectangle : TRect;
begin
if Column.Field.DataType = ftBoolean then
begin
DBGrid1.Canvas.FillRect(Rect);
CheckBoxRectangle.Left := Rect.Left + 2;
CheckBoxRectangle.Right := Rect.Right - 2;
CheckBoxRectangle.Top := Rect.Top + 2;
CheckBoxRectangle.Bottom := Rect.Bottom - 2;
DrawFrameControl(DBGrid1.Canvas.Handle,
CheckBoxRectangle,
DFC_BUTTON,
CtrlState[Column.Field.AsBoolean]);
end;
end;
procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
if DBGrid1.SelectedField.DataType = ftBoolean then
begin
Self.FOriginalOptions := DBGrid1.Options;
DBGrid1.Options := DBGrid1.Options - [dgEditing];
end;
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.DataType = ftBoolean then
DBGrid1.Options := Self.FOriginalOptions;
end;
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
if DBGrid1.SelectedField.DataType = ftBoolean then
SaveBoolean();
end;
Syntax Highlighted with https://pascal-id.org/syntax
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com