CheckBox within a DBGrid

1

I have DBGrid in my project and I need it to have two columns with CheckBox . This project is failing in VCL Form and I use Firebird 2.5 database that does not have Boolean fields. For checking the fields I wanted to do something like this:

if (Column.Field.Value <> '') or (Column.Field.Value = Null) then
   CheckBox.Checked := True;

Does anyone know of any way to do this without using Third Party components?

    
asked by anonymous 19.12.2017 / 22:04

1 answer

3

Friend, I created a ClientDataSet with 2 fields, a string calling name and another integer named selection.

Adding some records in formcreate

 ClientDataSet1.Open;
 ClientDataSet1.Append;
 ClientDataSet1Nome.AsString := 'Stack';
 ClientDataSet1Selecao.AsString := '1';
 ClientDataSet1.Append;
 ClientDataSet1Nome.AsString := 'OverFlow';
 ClientDataSet1Selecao.AsString := '0';
 ClientDataSet1.Post;

In the DrawColumnCell event of the grid place the following code:

var
  Check: Integer;
  R: TRect;
begin
  inherited;

  if ((Sender as TDBGrid).DataSource.Dataset.IsEmpty) then
    Exit;

  if(UpperCase(Column.FieldName) = 'SELECAO') then
  begin
    TDBGrid(Sender).Canvas.FillRect(Rect);
    if (TDBGrid(Sender).DataSource.DataSet.FieldByName('selecao').AsInteger = 1) then
      Check := DFCS_CHECKED
    else
      Check := 0;
    R := Rect;
    InflateRect(R, -2, -2);
    DrawFrameControl(TDBGrid(Sender).Canvas.Handle, R, DFC_BUTTON,
      DFCS_BUTTONCHECK or Check);
  end;
end;

In Grid ColEnter

if UpperCase(TDBGrid(Sender).SelectedField.FieldName) = 'SELECAO' then
  TDBGrid(Sender).Options := TDBGrid(Sender).Options - [dgEditing]
else
  TDBGrid(Sender).Options := TDBGrid(Sender).Options + [dgEditing];

In the CellClick of the grid

 if(UpperCase(Column.FieldName) = 'SELECAO') then
 begin
   ClientDataSet1.Edit;
   if(ClientDataSet1.FieldByName('Selecao').AsInteger = 1) then
   begin
    ClientDataSet1.FieldByName('Selecao').AsInteger := 0;
   end
   else
   begin
    ClientDataSet1.FieldByName('selecao').AsInteger := 1;
   end;
   ClientDataSet1.Post;
   ShowMessage(ClientDataSet1.FieldByName('selecao').AsString);
 end;

In the GetText event of the field the ClientDataSet check will be

 Text := EmptyStr;
    
20.12.2017 / 01:51