Anda di halaman 1dari 6

Untuk membuat interfacenya di Delphi kita tampilkan dbgrid invoice,

lalu dipilih mana-mana yang mau dikirim saat itu, kemudian setelah dipilih maka
akan tampil form pengiriman yang terdiri atas invoice-invoicenya yang dipilih
tersebut.
Permasalahannya bagaimanakah cara membuat checkbox didbgrid tersebut?

Caranya adalah sbb

Klik komponen DBGRID


pada event double klik oncellclick, lalu ketikan kode dibawah ini

if Self.DBGrid.SelectedField.DataType = ftBoolean then


SaveBoolean();

pada event oncolenter, ketikan kode dibawah ini

if Self.DBGrid.SelectedField.DataType = ftBoolean then


begin
Self.FOriginalOptions := Self.DBGrid.Options;
Self.DBGrid.Options := Self.DBGrid.Options - [dgEditing];
end;

Pada event oncolexit, ketikan kode dibawah ini

if Self.DBGrid.SelectedField.DataType = ftBoolean then


Self.DBGrid.Options := Self.FOriginalOptions;

Pada event DrawColumnCell, kodenya sbb, ikuti dan sesuaikan

procedure TNamaForm.DBGridDrawColumnCell(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
Self.DBGrid.Canvas.FillRect(Rect);
CheckBoxRectangle.Left := Rect.Left + 2;
CheckBoxRectangle.Right := Rect.Right - 2;
CheckBoxRectangle.Top := Rect.Top + 2;
CheckBoxRectangle.Bottom := Rect.Bottom - 2;
DrawFrameControl(Self.DBGrid.Canvas.Handle,
CheckBoxRectangle,
DFC_BUTTON,
CtrlState[Column.Field.AsBoolean]);
end;
end;

Lalu buatlah Procedure dengan nama SaveBoolean (jangan lupa deklarasikan juga)

procedure TNamaForm.SaveBoolean;
var pilihan:integer;
begin
//contoh disini nama datasourcenya DsListInvoice
if not(DsListInvoice.DataSet.State in [dsEdit]) then
DsListInvoice.DataSet.Edit;
if Self.DBGrid.SelectedField.AsBoolean=false then
begin
{pada saat membuat tabel yang Anda inginkan, tambahkan kolom dengan nama
selected untuk flag pilih ya atau tidak, disini 1 berarti true dan 0 berarti false}
Table1selected.AsInteger:=1;
end
else
Table1selected.AsInteger:=0;
end;

Contoh tersebut bisa diterapkan di aplikasi lain yang membutuhkan checkbox pada
DBGRID di Delphi

OK Selamat mencoba

##########
##########
##########

//OnCellClik event of a DBGrid1


procedure TForm.DBGrid1CellClick(Column: TColumn) ;
begin
if (Column.Field.DataType=ftBoolean) then
begin
{toggle True and False}
Column.Grid.DataSource.DataSet.Edit;
Column.Field.Value:= not Column.Field.AsBoolean;
{immediate post - see for yourself whether you want this}
Column.Grid.DataSource.DataSet.Post;
{you may add additional functionality here, to be processed after the change
was made}
end;
end;
//OnDrawColumnCell event of a DBGrid1
procedure TForm.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) ;
begin
if (Column.Field.DataType=ftBoolean) then
begin
DBGrid1.Canvas.FillRect(Rect) ;
if VarIsNull(Column.Field.Value) then
DrawFrameControl(DBGrid1.Canvas.Handle,Rect, DFC_BUTTON, DFCS_BUTTONCHECK or
DFCS_INACTIVE) {grayed}
else
DrawFrameControl(DBGrid1.Canvas.Handle,Rect, DFC_BUTTON,
CtrlState[Column.Field.AsBoolean]) ; {checked or unchecked}
end;
end;

##########
##########
##########
Bikin Checkbox di Dbgrid
Caranya adalah sbb

---------------------------------------------------------------------------
private
FOriginalOptions : TDBGridOptions;
procedure SaveBoolean;

----------------------------------------------------------------------------
procedure Tfrm_vtgh.SaveBoolean;
begin

Self.crDBGrid1.SelectedField.Dataset.Edit;

Self.crDBGrid1.SelectedField.AsBoolean := not
Self.crDBGrid1.SelectedField.AsBoolean;
if Self.crDBGrid1.SelectedField.AsBoolean then
Self.CRDBGrid1.Fields[0].Value:='Tertagih'
else Self.CRDBGrid1.Fields[0].Value:='';

Self.crDBGrid1.SelectedField.Dataset.Post;

end;

---------------------------------------------------------------------------
pada event oncolenter, ketikan kode dibawah ini

if Self.crDBGrid1.SelectedField.DataType = ftBoolean then


begin
Self.FOriginalOptions := Self.crDBGrid1.Options;
Self.crDBGrid1.Options := Self.crDBGrid1.Options -[dgEditing];
end;

----------------------------------------------------------------------
Pada event oncolexit, ketikan kode dibawah ini

if Self.crDBGrid1.SelectedField.DataType = ftBoolean then


Self.crDBGrid1.Options := Self.FOriginalOptions;

----------------------------------------------------------------------
Pada event DrawColumnCell, kodenya sbb, ikuti dan sesuaikan

Const
CtrlState : array[Boolean] of Integer = (DFCS_BUTTONCHECK,DFCS_BUTTONCHECK or
DFCS_CHECKED);
var
CheckBoxRectangle : TRect;
begin
if Column.Field.DataType = ftBoolean then
begin
Self.crDBGrid1.Canvas.FillRect(Rect);
CheckBoxRectangle.Left := Rect.Left + 2;
CheckBoxRectangle.Right := Rect.Right - 2;
CheckBoxRectangle.Top := Rect.Top + 2;
CheckBoxRectangle.Bottom := Rect.Bottom - 2;
DrawFrameControl(Self.crDBGrid1.Canvas.Handle,CheckBoxRectangle,DFC_BUTTON,
CtrlState[Column.Field.AsBoolean]);
end;
end;

##########
##########
##########

Langkah pertama adalah mendeklarasikan List Array Of Boolean sebagai Variable .

private
{ Private declarations }
aList : array of boolean;

Pada eventOnDrawColumnCell tambahkan Code

if cmdView.RecordCount>0 then// Validasi Jika Ada Record. Jika Validasi ini tidak
dilakukan akan error pada saat record 0
begin

if Column.Index = 0 then begin//If it's the right column.


if aList[cmdView.RecNo-1] = True then
DrawFrameControl(dgHistory.Canvas.Handle, Rect, DFC_BUTTON, DFCS_CHECKED)
else
DrawFrameControl(dgHistory.Canvas.Handle, Rect, DFC_BUTTON, DFCS_BUTTONCHECK);
end;
end;

Anda Bisa melakukan Event ini Pada Event OnCellClik atau DblClick

if cmdView.RecordCount>0 then
begin
if dgHistory.SelectedIndex = 0 then
begin
aList[cmdView.RecNo-1] := NOT aList[cmdView.RecNo-1];
dgHistory.Invalidate;
end;
end;

Untuk Melakukan Selection Record dapat dengan loop Pada Data


cmdView.First;
while not cmdView.Eof do
begin
if aList[cmdView.RecNo-1]= True Then
// Process
cmdView.Next;
end;

##########
##########
##########

CheckBox in a DBGrid

To show a checkbox inside a cell of a DBGrid, we'll need to make one available for
us at run time.
Select the "Data controls" page on the Component Palette and pick a TDBCheckbox.
Drop one anywhere on the form - it doesn't matter where, since most of the time it
will be invisible or floating over the grid.

Tip: TDBCheckBox is a data-aware control that allows the user to select or deselect
a single value, which is appropriate for boolean fields.

Next, set its Visible property to False. Change the Color property of DBCheckBox1
to the same color as the DBGrid (so it blends in with the DBGrid) and remove the
Caption.

Most importantly, make sure the DBCheckBox1 is connected to the DataSource1 and to
the correct field.

Note that all the above DBCheckBox1's property values can be set in the form's
OnCreate event like this:

procedure TForm1.FormCreate(Sender: TObject);


begin
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.DataField := 'Winner';
DBCheckBox1.Visible := False;
DBCheckBox1.Color := DBGrid1.Color;
DBCheckBox1.Caption := '';

//explained later in the article


DBCheckBox1.ValueChecked := 'Yes a Winner!';
DBCheckBox1.ValueUnChecked := 'Not this time.';
end;

What comes next is the most interesting part. While editing the boolean field in
the DBGrid, we need to make sure the DBCheckBox1 is placed above ("floating") the
cell in the DBGrid displaying the boolean field.

For the rest of the (non-focused) cells carrying the boolean fields (in the
"Winner" column), we need to provide some graphical representation of the boolean
value (True/False). This means you need at least two images for drawing: one for
the checked state (True value) and one for the unchecked state (False value).

The easiest way to accomplish this is to use the Windows API DrawFrameControl
function to draw directly on the DBGrid's canvas.

Here's the code in the DBGrid's OnDrawColumnCell event handler that occurs when the
grid needs to paint a cell.

procedure TForm1.DBGrid1DrawColumnCell(
Sender: TObject; const Rect: TRect; DataCol:
Integer; Column: TColumn; State: TGridDrawState);

const IsChecked : array[Boolean] of Integer =


(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
DrawState: Integer;
DrawRect: TRect;
beginif (gdFocused in State) thenbeginif (Column.Field.FieldName =
DBCheckBox1.DataField) thenbegin
DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox1.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox1.Width := Rect.Right - Rect.Left;
DBCheckBox1.Height := Rect.Bottom - Rect.Top;
DBCheckBox1.Visible := True;
endendelsebeginif (Column.Field.FieldName = DBCheckBox1.DataField) thenbegin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
end;
end;

To finish this step, we need to make sure DBCheckBox1 is invisible when we leave
the cell:

procedure TForm1.DBGrid1ColExit(Sender: TObject);


beginif DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
DBCheckBox1.Visible := False
end;

We need just two more events to handle.

Note that when in editing mode, all keystrokes are going to the DBGrid's cell, we
have to make sure they are sent to the CheckBox. In the case of a CheckBox we are
primarily interested in the [Tab] and the [Space] key. [Tab] should move the input
focus to the next cell, and [Space] should toggle the state of the CheckBox.

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


beginif (key = Chr(9)) then Exit;
if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) thenbegin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
end;
end;

It could be appropriate for the Caption of the checkbox to change as the user
checks or unchecks the box. Note that the DBCheckBox has two properties
(ValueChecked and ValueUnChecked) used to specify the field value represented by
the checkbox when it is checked or unchecked.

This ValueChecked property holds "Yes, a Winner!", and ValueUnChecked equals "Not
this time."

procedure TForm1.DBCheckBox1Click(Sender: TObject);


beginif DBCheckBox1.Checked then
DBCheckBox1.Caption := DBCheckBox1.ValueChecked
else
DBCheckBox1.Caption := DBCheckBox1.ValueUnChecked;
end;

Run the project and you'll see the checkboxes all over the Winner field's column.

##########
##########
##########

https://mboloz.wordpress.com/2013/10/29/multi-delete-dengan-dbgrideh/

Anda mungkin juga menyukai