(Acentos omitidos a proposito)
Neftali tiene razon, el tema se ha tratado varias veces. He probado mucha soluciones y ninguna funciona perfectamente.
Esto lo encontré en la web y es del año 2002 pero es excelente.
Para probarlo puse una DbGrid y un ClientDataSet, cree los campos SEL(boolean) Y DESCRIPCION(string). Luego CreateDataSet y ejecute.
La primera vez por supuesto salta el error de que no halla el archivo 'base'.
Cargue algunos datos en Descripcion y Cerre.
Volvi a abrir y PERFECTO.
Espero que les sirva, es mas de lo mismo pero anda.
Felices Fiestas.
Código Delphi
[-]unit Ubase;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, DBClient;
type
TForm1 = class(TForm)
cds: TClientDataSet;
ds: TDataSource;
DBGrid: TDBGrid;
cdssel: TBooleanField;
cdsDescripcion: TStringField;
cdsPrecio: TCurrencyField;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure DBGridCellClick(Column: TColumn);
procedure DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGridColEnter(Sender: TObject);
procedure DBGridColExit(Sender: TObject);
private
FOriginalOptions : TDBGridOptions;
procedure SaveBoolean;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
cds.LoadFromFile('base');
Self.FOriginalOptions:= Self.DBGrid.Options;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
cds.SaveToFile('base');
end;
procedure TForm1.DBGridCellClick(Column: TColumn);
begin
if Self.DBGrid.SelectedField.DataType = ftBoolean then
SaveBoolean();
end;
procedure TForm1.SaveBoolean;
begin
Self.DBGrid.SelectedField.Dataset.Edit;
Self.DBGrid.SelectedField.AsBoolean := not Self.DBGrid.SelectedField.AsBoolean;
Self.DBGrid.SelectedField.Dataset.Post;
end;
procedure TForm1.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
Const
CtrlState : array[Boolean] of Integer = (DFCS_BUTTONCHECK,
DFCS_BUTTONCHECK or DFCS_CHECKED);
var
CheckBoxRectangle : TRect;
begin
if Column.Field.DataType = ftBoolean then
begin
Self.DBGrid.Canvas.FillRect(Rect);
CheckBoxRectangle.Left := Rect.Left + 2;
CheckBoxRectangle.Right := Rect.Right - 2;
CheckBoxRectangle.Top := Rect.Top + 2;
CheckBoxRectangle.Bottom := Rect.Bottom - 2;
DrawFrameControl(Self.DBGrid.Canvas.Handle,
CheckBoxRectangle,
DFC_BUTTON,
CtrlState[Column.Field.AsBoolean]);
end;
end;
procedure TForm1.DBGridColEnter(Sender: TObject);
begin
if Self.DBGrid.SelectedField.DataType = ftBoolean then
begin
Self.FOriginalOptions := Self.DBGrid.Options;
Self.DBGrid.Options := Self.DBGrid.Options - [dgEditing];
end;
end;
procedure TForm1.DBGridColExit(Sender: TObject);
begin
if Self.DBGrid.SelectedField.DataType = ftBoolean then
Self.DBGrid.Options := Self.FOriginalOptions;
end;
end.