Ask Your Question
1

How can I show a checkmark in a Boolean field in Delphi's DBGrid?

asked 2022-03-26 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-03-10 11:00:00 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-26 11:00:00 +0000

Seen: 19 times

Last updated: Mar 10 '23