An answer to your immediate problem is to use code like this:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
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
else begin
DBGrid1.DefaultDrawDataCell(Rect, Column.Field, State);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DBGrid1.Options := DBGrid1.Options + [dgEditing];
DBGrid1.DefaultDrawing := False;
CDS1.CreateDataSet;
CDS1.InsertRecord([1, True]);
CDS1.InsertRecord([2, False]);
CDS1.First;
end;
(CDS1 is a TClientDataSet with an ftAutoInc field and an ftBoolean one)
As you'll see, setting the grid's DefaultDrawing
to False prevents the Boolean
field's Caption in the DBGrid from being drawn.
Don't get your hopes up too much,
though, because if you click in one of the checkboxs, you'll immediately see the
problem with this limited implementation of checkbox support: Clicking the checkbox
activates the grid's inplace editor, which replaces the checkbox image by the
editing text for an ftBoolean field, namely 'True' or 'False. However, your q
didn't ask about editing so this answer should stop here, I think.
If you want fuller checkbox support, just google
delphi dbgrid checkbox
and, in the first hit,
CheckBox in a DBGrid
the second answer should tell you a bit more. Also, I think you'll find that quite a lot of the 3rd-party DBGrid replacements include support for checkboxes, etc.