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;