Checkbox within a Dbgrid

Asked

Viewed 1,943 times

1

I have a DBGrid in my project and I need it to have two columns with CheckBox. This project is aimed at VCL Form and use Firebird 2.5 database that has no fields Boolean. For the verification of the fields I intended to do something like this:

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

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

1 answer

4


Friend, I created a Clientdataset with 2 fields, a string calling name and another integer called selector.

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 grid Drawcolumncell event put 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 the 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 field event that will be the Clientdataset check (Select)

 Text := EmptyStr;
  • 1

    After I tested I realized I just need the event DrawColumnCell because I won’t need to change the image on Click. Thank you very much for the Help.

  • 1

    You know how to mark the desired items only with the mouse?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.