Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to show a checkmark in a Boolean field in Delphi's DBGrid is to use a TDBComboBox control instead of a TDBCheckBox control in the grid's column editor. Set the combobox's Items property to "Yes" and "No", and its Style property to "csDropDownList". Then, use the OnDrawColumnCell event of the grid to draw a checkmark symbol in the cell when the field value is True. Here's an example code:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var CheckRect: TRect; begin if Column.Field.DataType = ftBoolean then begin CheckRect := Rect; InflateRect(CheckRect, -2, -2); DrawFrameControl(DBGrid1.Canvas.Handle, CheckRect, DFCBUTTON, DFCSBUTTONCHECK or ( Ord(Column.Field.AsBoolean) * DFCS_CHECKED ) ); end else DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

This code checks if the column's field is a Boolean type, and if so, it draws a checkmark or an unchecked box in the column cell depending on its value. The InflateRect function is used to create a smaller rectangle inside the cell's rectangle, where the checkmark will be drawn. The DrawFrameControl function is a Windows API function that draws a checkbox frame with a checkmark or an unchecked box, depending on the DFCS_CHECKED flag passed to it.