H
hwp689
Unregistered / Unconfirmed
GUEST, unregistred user!
添加DBCheckBox1,ADOTabel1,DBGrid1,ADOConnection1,DataSource1控键,数据库中创建一个Bit类型字段,下例为‘sex’;数据能实时更新;DBGrid:
procedure TForm1.FormShow(Sender: TObject);
begin
ADOTable1.Active:=true;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
DrawState: Integer;
DrawRect: TRect;
begin
//1
if (gdFocused in State) then
begin
//2
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
//
DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox1.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox1.Width := Rect.Right - Rect.Left;
DBCheckBox1.Height := Rect.Bottom - Rect.Top;
DBCheckBox1.Visible := True;
end;
//3
end;
///2
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
//2
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
//2
end;
//1
procedure TForm1.FormCreate(Sender: TObject);
begin
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.DataField := 'sex';
DBCheckBox1.Visible := False;
DBCheckBox1.Color := DBGrid1.Color;
DBCheckBox1.Caption := '';
DBCheckBox1.ValueChecked := '';
DBCheckBox1.ValueUnChecked := '';
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject;
var Key: Char);
begin
if (key = Chr(9)) then
Exit;
if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) then
begin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
end;
end;
procedure TForm1.DBGrid1Exit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
DBCheckBox1.Visible := False
end;
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
ADOTable1.Cancel;
if Column.FieldName = DBCheckBox1.DataField then
begin
ADOTable1.Edit;
ADOTable1.FieldByName('sex').AsBoolean:= not ADOTable1.FieldByName('sex').AsBoolean;
ADOTable1.Post;
end;
end;
procedure TForm1.DBCheckBox1Click(Sender: TObject);
begin
if DBCheckBox1.Checked then
DBCheckBox1.Caption := DBCheckBox1.ValueChecked
else
DBCheckBox1.Caption := DBCheckBox1.ValueUnChecked;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ADOTable1.Active:=true;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
DrawState: Integer;
DrawRect: TRect;
begin
//1
if (gdFocused in State) then
begin
//2
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
//
DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox1.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox1.Width := Rect.Right - Rect.Left;
DBCheckBox1.Height := Rect.Bottom - Rect.Top;
DBCheckBox1.Visible := True;
end;
//3
end;
///2
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
//2
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
//2
end;
//1
procedure TForm1.FormCreate(Sender: TObject);
begin
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.DataField := 'sex';
DBCheckBox1.Visible := False;
DBCheckBox1.Color := DBGrid1.Color;
DBCheckBox1.Caption := '';
DBCheckBox1.ValueChecked := '';
DBCheckBox1.ValueUnChecked := '';
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject;
var Key: Char);
begin
if (key = Chr(9)) then
Exit;
if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) then
begin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
end;
end;
procedure TForm1.DBGrid1Exit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
DBCheckBox1.Visible := False
end;
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
ADOTable1.Cancel;
if Column.FieldName = DBCheckBox1.DataField then
begin
ADOTable1.Edit;
ADOTable1.FieldByName('sex').AsBoolean:= not ADOTable1.FieldByName('sex').AsBoolean;
ADOTable1.Post;
end;
end;
procedure TForm1.DBCheckBox1Click(Sender: TObject);
begin
if DBCheckBox1.Checked then
DBCheckBox1.Caption := DBCheckBox1.ValueChecked
else
DBCheckBox1.Caption := DBCheckBox1.ValueUnChecked;
end;