Reputation: 810
I have my application in Delphi 10.1. We have used component DBGrid on the form. I wanted to add checkbox in the first column of the grid. So added following code on drawcell of grid:
const
CtrlState: array [boolean] of integer =
(DFCS_BUTTONCHECK,DFCS_BUTTONCHECK
or DFCS_CHECKED);
var
style: UINT;
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;
Checkbox gets added to first column but along with caption as shown below:
How do I remove this caption True or False?
Upvotes: 3
Views: 5508
Reputation: 157
I managed to solve this by assigning DisplayValues property of my boolean field to empty values like TField.DisplayValues := ';'.
Note that you can change what it will show in your DBGrid cell instead of True/False. For example, you can change it to show Yes/No by setting TField.DisplayValues := 'Yes;No'. So if you set it to empty values it will show nothing.
P.S. I am using Delphi XE3.
Upvotes: 2
Reputation: 30715
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,
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.
Upvotes: 4