Tuesday, May 19, 2020

Edit and Display Boolean Fields using a CheckBox in Delphi

Tip submitted by Rene van der Heijden A series of articles titled Adding components to a DBGrid discusses placing just about any Delphi control (visual component) into a cell of a DGBrid. The idea is to create visually more attractive user interfaces for editing fields inside a DBGrid: a ComboBox for drop down lists; a DateTimePicker (calendar) for date values; a check box for boolean fields. CheckBox for Boolean Fields CheckBox inside a DBGrid As noticed by Rene van der Heijden the solution is rather lengthy, and it doesnt work, at least not when using the mouse to click on the checkboxes. Rene suggest an easier approach needing only two even handlers: OnCellClick and OnCustomDrawCell for your DBGrid control: //OnCellClik event of a DBGrid1 procedure TForm.DBGrid1CellClick(Column: TColumn) ; begin   Ã‚  if (Column.Field.DataTypeftBoolean) then   Ã‚  begin   Ã‚  Ã‚  Ã‚  {toggle True and False}   Ã‚  Ã‚  Ã‚  Column.Grid.DataSource.DataSet.Edit;   Ã‚  Ã‚  Ã‚  Column.Field.Value: not Column.Field.AsBoolean;   Ã‚  Ã‚  {immediate post - see for yourself whether you want this}   Ã‚  Ã‚  Ã‚  Column.Grid.DataSource.DataSet.Post;   Ã‚  Ã‚  Ã‚  {you may add additional functionality here,   Ã‚  Ã‚  to be processed after the change was made}   Ã‚  end; end; //OnDrawColumnCell event of a DBGrid1 procedure TForm.DBGrid1DrawColumnCell(   Ã‚  Sender: TObject;   Ã‚  const Rect: TRect;   Ã‚  DataCol: Integer;   Ã‚  Column: TColumn;   Ã‚  State: TGridDrawState) ; const   Ã‚  CtrlState: array[Boolean] of integer (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED) ; begin   Ã‚  if (Column.Field.DataTypeftBoolean) 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; end; Delphi tips navigator: » Remove Duplicate Items in Delphis TStringList « 5 Facts you Did Not Know about Delphi and Classes and the VCL and Inheritance and Custom Controls and...

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.