可以实现:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Grids, DBGridEh, DB, ADODB, ImgList;
type
TForm1 = class(TForm)
DBGridEh1: TDBGridEh;
BitBtn1: TBitBtn;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
ImageList1: TImageList;
BitBtn2: TBitBtn;
procedure DBGridEh1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumnEh;
State: TGridDrawState);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ChkOnClick(Sender: TObject);
end;
var
Form1: TForm1;
ChksState : array of boolean;
//全程数组,存放每条记录的CheckBox状态
implementation
{$R *.dfm}
procedure TForm1.DBGridEh1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumnEh;
State: TGridDrawState);
var
Chk : TCheckBox;
begin
if (TDBGridEh(Sender).DataSource.DataSet.Active) and (DataCol = 0) then
begin
Chk := TCheckBox.Create(self);
Chk.Left := Rect.Left +TDBGridEh(Sender).Left + 2;
Chk.Top := Rect.Top + TDBGridEh(Sender).top + 2;
Chk.Height := Rect.Bottom - Rect.Top - 2;
Chk.Width :=Rect.Right - Rect.Left - 14;// Chk.Height;
Chk.Color := clGreen;//
Chk.Tag:= TDBGridEh(Sender).DataSource.DataSet.RecNo-1 ;
if ChksState[Chk.Tag] then
Chk.Checked := true else
Chk.Checked := false;
Chk.OnClick:= ChkOnClick;
Chk.Parent := self;
Chk.Visible := true;
Refresh;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i:integer;
Rect: TRect;
Chk : TCheckBox;
begin
ADOQuery1.Active:=true;
SetLength(ChksState,DataSource1.DataSet.RecordCount);
for i := 0 to DataSource1.DataSet.FieldCountdo
DBGridEh1.Columns.Add;
DBGridEh1.Columns[0].Title.Caption := '';
for i := 1 to DataSource1.DataSet.FieldCountdo
begin
DBGridEh1.Columns.Field := DataSource1.DataSet.Fields[i-1];
DBGridEh1.Columns.Title.Caption :=DataSource1.DataSet.Fields[i-1].FullName;
end;
end;
procedure TForm1.ChkOnClick(Sender: TObject);
var
ch:TCheckBox;
i:integer;
begin
// ch:=TCheckBox(Sender);
if TCheckBox(Sender).Checked then
//DataSource1.DataSet.RecNo-1
ChksState[TCheckBox(Sender).Tag] := true
else
ChksState[TCheckBox(Sender).Tag] := false;
end;