Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB;
Type
TForm1 = Class(TForm)
DataSource1: TDataSource;
ADOTable1: TADOTable;
DBGrid1: TDBGrid;
Procedure DBGrid1DrawColumnCell(Sender: TObject; Const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
Procedure DBGrid1ColEnter(Sender: TObject);
Procedure DBGrid1ColExit(Sender: TObject);
Procedure DBGrid1CellClick(Column: TColumn);
Procedure DBGrid1KeyDown(Sender: TObject; Var Key: Word;
Shift: TShiftState);
private
OriginalOptions: TDBGridOptions;
Procedure SaveBoolean;
{ Private declarations }
public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
{$R *.dfm}
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);
// CtrlString:=string;
Begin
//确保只有在逻辑字段才能插入组件
If Column.Field.DataType = ftBoolean Then
Begin
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, Rect, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]); //画框框
// Column.Field.Text:='google';
End;
End;
Procedure TForm1.DBGrid1ColEnter(Sender: TObject);
Begin
// 确保该栏是逻辑字段
If DBGrid1.SelectedField.DataType = ftBoolean Then
Begin
OriginalOptions := DBGrid1.Options;
DBGrid1.Options := DBGrid1.Options - [dgEditing];
End;
End;
Procedure TForm1.DBGrid1ColExit(Sender: TObject);
Begin
//确保该栏是逻辑字段
If DBGrid1.SelectedField.DataType = ftBoolean Then
DBGrid1.Options := OriginalOptions;
End;
Procedure TForm1.DBGrid1CellClick(Column: TColumn);
Begin
//确保该栏是逻辑字段
If DBGrid1.SelectedField.DataType = ftBoolean Then
SaveBoolean();
End;
Procedure TForm1.DBGrid1KeyDown(Sender: TObject; Var Key: Word;
Shift: TShiftState);
Begin
//确保该栏是逻辑字段和空格键在键盘中被敲击
If (Key = VK_SPACE) And (DBGrid1.SelectedField.DataType = ftBoolean) Then
SaveBoolean();
End;
Procedure TForm1.SaveBoolean;
Begin
DBGrid1.SelectedField.Dataset.Edit;
DBGrid1.SelectedField.AsBoolean := Not DBGrid1.SelectedField.AsBoolean;
DBGrid1.SelectedField.Dataset.Post;
End;
End.