CheckBox in DBGrid

My question is how to set a column in dbgrid in Delphi 7 which will be with checkbox elements.

Thanks in advance.

+4
source share
4 answers

OK I used this article for my problem. OK But the problem is that it does not work, as it should be. Therefore, I change my logic in the code. And implementing it, keeping the selected rows from dbgrid in the list.

0
source

The simplest and most comprehensive method tested by me is as follows:

In the private section of your unit, declare a global value to save the grid settings. It will be used to recover after temporarily disabling text editing while entering a checkbox column - as this is probably one of the few errors mentioned by Jordan Borisov regarding the article delphi.about.com

private GridOriginalOptions : TDBGridOptions; 

In the OnCellClick event, if the field is logical, toggles and publishes changes to the database

 procedure TForm1.DBGrid1CellClick(Column: TColumn); begin if (Column.Field.DataType=ftBoolean) then begin Column.Grid.DataSource.DataSet.Edit; Column.Field.Value:= not Column.Field.AsBoolean; Column.Grid.DataSource.DataSet.Post; end; end; 

Drawing a flag for Boolean grid fields

 procedure TForm1.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.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) else DrawFrameControl(DBGrid1.Canvas.Handle,Rect, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]); end; end; 

Now the new part will disable editing cells in the boolean column. In the OnColEnter and OnColExit events:

 procedure TForm1.DBGrid1ColEnter(Sender: TObject); begin if Self.DBGrid1.SelectedField.DataType = ftBoolean then begin Self.GridOriginalOptions := Self.DBGrid1.Options; Self.DBGrid1.Options := Self.DBGrid1.Options - [dgEditing]; end; end; procedure TForm1.DBGrid1ColExit(Sender: TObject); begin if Self.DBGrid1.SelectedField.DataType = ftBoolean then Self.DBGrid1.Options := Self.GridOriginalOptions; end; 

Moreover, the space bar key to toggle the checkbox

 procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if ((Self.DBGrid1.SelectedField.DataType = ftBoolean) and (key = VK_SPACE)) then begin Self.DBGrid1.DataSource.DataSet.Edit; Self.DBGrid1.SelectedField.Value:= not Self.DBGrid1.SelectedField.AsBoolean; Self.DBGrid1.DataSource.DataSet.Post; end; end; 

What is it!

+8
source

If you use TClientDataset + TDatasetProvider + TDataset, you can manipulate a variant of the data array before it gets into clientdataset and includes a non-updatable logical field.

Once everything is done, you just need to draw a grid using the OnDrawColumnCell event. Here I did not use CheckBox, but just a bitmap (when the user clicks on it, it changes to selected / unselected).

+1
source

Please excuse me for posting this answer, I do not have 50 reputation to add comments.

Mihai MATEI's answer is very close to a rare (as in a really working) solution, except in the case of use where it is mistaken.

Whenever the first user action in the grid should click on this check box, the first click will work, and the second will open the basic DBGrid editor.

This is because the "GridOriginalOptionsmechan" mechanism must be initialized. To do this, simply add the following code to the grid. OnEnter Event:

 procedure TForm1.DBGrid1Enter(Sender: TObject); begin DBGrid1ColEnter(Sender); end; 

What is it!

0
source

Source: https://habr.com/ru/post/1393185/


All Articles