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?
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
##########
##########
##########
##########
##########
##########
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
----------------------------------------------------------------------
Pada event oncolexit, ketikan kode dibawah ini
----------------------------------------------------------------------
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;
##########
##########
##########
private
{ Private declarations }
aList : array of boolean;
if cmdView.RecordCount>0 then// Validasi Jika Ada Record. Jika Validasi ini tidak
dilakukan akan error pada saat record 0
begin
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;
##########
##########
##########
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:
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);
To finish this step, we need to make sure DBCheckBox1 is invisible when we leave
the cell:
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.
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."
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/