如何将DBcheckbox嵌入到DBgrid中去,并且与数据库中的某一逻辑型字段对应关联起来? 我的要求是如果数据库表的逻辑型字段记录为true 那末在Dbgrid中的Dbcheckbox 就为打勾状态,并且改变Dbcheckbox 的状态就直接改变了数据库表的逻辑字段
如何将DBcheckbox嵌入到DBgrid中去,并且与数据库中的某一逻辑型字段对应关联起来? 我的要求是如果数据库表的逻辑型字段记录为true 那末在Dbgrid中的Dbcheckbox 就为打勾状态,并且改变Dbcheckbox 的状态就直接改变了数据库表的逻辑字段
VirtualTree
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, DB, DBTables, Grids, DBGrids;type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
DBComboBox1: TDBComboBox;
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if ( Field.FieldName = DBComboBox1.DataField ) then
begin
DBComboBox1.Left := Rect.Left + DBGrid1.Left;
DBComboBox1.Top := Rect.Top + DBGrid1.top;
DBComboBox1.Width := Rect.Right - Rect.Left;
DBComboBox1.Height := Rect.Bottom - Rect.Top;
DBComboBox1.Visible := True;
end;
end;
end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
begin
DBComboBox1.Visible := false;
end;
end;procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName=DBComboBox1.DataField) then
begin
DBComboBox1.SetFocus;
SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0);
end;
end;
end;procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('SIZE').AsInteger = 2 then
begin
DBGrid1.Canvas.Font.Color:=clred;
DBGrid1.Canvas.Brush.color:=clyellow;
end else
begin
DBGrid1.Canvas.Font.Color:=clblue;
DBGrid1.Canvas.Brush.color:=clBtnFace;
end;
DBGrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;end.
你换一下控件,而且在GetData里判断,给它进行Checked给值