From: http://rabbitfox.blog.sohu.com/33264033.html
http://community.csdn.net/Expert/topic/5342/5342920.xml?temp=.9525568
Q: How to make a similar increase dbgrid checkbox controls, you can use the mouse to select whether the row is selected, multiple choice
The best idea is to add a Boolean field in the data table, then DBCheckBox associated with it, rather than CheckBox. (As for the method of embedding DBCheckBox DBGrid, the reference may be: http://topic.csdn.net/t/20021118/16/1186480.html )
If you just do not want to increase the Boolean field in a data table, just want to use CheckBox, the following is my test code for reference:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DB, Grids, DBGrids, DBTables;
of the type
TForm1 = class (TForm)
Table1: TTable; // here to do a test with a Table. With other DataSet, such as Query, ADOQuery etc. as
DBGrid1: the TDBGrid;
DataSource1: TDataSource; // DataSource1 if the name change, along with the following code to replace the
Button1: TButton;
Button2: TButton;
Procedure Button1Click (Sender: TObject);
Procedure Button2Click (Sender: TObject);
Procedure DBGrid1DrawColumnCell (Sender: TObject; const Rect: The TRect;
DataCol: Integer; the Column: TColumn; State: TGridDrawState);
Procedure the FormCreate (Sender: TObject);
Private
{Private Declarations}
public
{public Declarations}
procedure ChkOnClick (Sender: TObject); // Note that
end;
var
the Form1: TForm1;
ChksState: Array of Boolean; // full array, store the state of each record CheckBox
implementation
{$R *.dfm}
procedure TForm1.ChkOnClick(Sender: TObject); //所有CheckBox的Click事件
begin
if TCheckBox(Sender).Checked then
ChksState[DataSource1.DataSet.RecNo-1] := true
else
ChksState[DataSource1.DataSet.RecNo-1] := false;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Chk : TCheckBox;
begin
if (TDBGrid(Sender).DataSource.DataSet.Active) and (DataCol = 0) then
begin
Chk := TCheckBox.Create(self);
Chk.Left := Rect.Left + TDBGrid(Sender).Left + 2;
Chk.Top := Rect.Top + TDBGrid(Sender).top + 2;
Chk.Height := Rect.Bottom - Rect.Top - 2;
Chk.Width := Chk.Height;
Chk.Color := clWhite;
if ChksState[TDBGrid(Sender).DataSource.DataSet.RecNo-1] then
Chk.Checked := true else Chk.Checked := false;
Chk.OnClick := ChkOnClick; //注意
Chk.Parent := Self;
Chk.Visible := true;
end;
end;
procedure TForm1.Button1Click(Sender: TObject); //打开数据源并构建DBGrid
var
I : integer;
begin
with DBGrid1 do
begin
DataSource := DataSource1;
DataSource.DataSet.Open;
SetLength(ChksState,DataSource.DataSet.RecordCount);
for I := 0 to DataSource.DataSet.FieldCount do
Columns.Add;
Columns[0].Title.Caption := 'Check';
for I := 1 to DataSource.DataSet.FieldCount do
begin
Columns[I].Field := DataSource.DataSet.Fields[I-1];
Columns[I].Title.Caption := 'Field_' + IntToStr(I);
end;
end;
end;
procedure TForm1.Button2Click (Sender: TObject); // test
the begin
IF ChksState [-DataSource1.DataSet.RecNo. 1] the then
ShowMessage ( '! = current state of the selected record')
the else
ShowMessage ( 'the current record is not selected');
End;
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.Close;
DataSource1.DataSet := Table1;
end;
end.
In this test, I used a Table, and its DataBaseName TableName property, the reader's own settings. In the test button Button2 in effect, write a method call Checked state: that is, by ChksState DataSet.RecNo array element corresponding to know whether the record is selected.
This is just a demo code. As can be seen, painstaking effort struggled so much code just to achieve so little function, and some special cases (such as: add, move, update, delete records) have not considered.
So, as far as possible do not use this DBGrid control ---> If it is to use, and to achieve similar functionality, it is the preferred number of tripartite control ---> If you do not want to use the tripartite control, try to put the effort in the DataSet ----> the most stupid way is, as I write this test the same: the focus was placed inside the control. This is actually take a detour.
http://www.cnblogs.com/hssbsw/archive/2012/06/15/2551438.html