在DBGrid增加一列CheckBox(而非DBCheckBox)

Delphi有很好用的DBNavitor元件,美中不足的是按钮的样式稍嫌不足!那是否可以仿DBNavigator的方式?笔者提供一个简单的范例给大家
:)delphi_simulate_dbnavigator一、自订首笔/上笔/下笔/末笔
procedure TForm1.DataSource1DataChange(Sender: TObject; Field:
TField);begin  BtnFirst.Enabled := not Datasource1.DataSet.Bof; 
BtnPrior.Enabled := not Datasource1.DataSet.Bof;  BtnNext.Enabled := not
Datasource1.DataSet.eof;  BtnLast.Enabled := not
Datasource1.DataSet.eof;  if TDataSource(Sender).State in
[dsEdit,dsInsert]  then begin    BtnFirst.Enabled := False;   
BtnPrior.Enabled := False;    BtnNext.Enabled := False;   
BtnLast.Enabled := False;  end;end;二、自订新增/修改/删除/取消/存档
procedure TForm1.DataSource1StateChange(Sender: TObject);begin 
BtnAdd.Enabled := TDataSource(Sender).State in [dsbrowse]; 
BtnEdit.Enabled := TDataSource(Sender).State in [dsbrowse]; 
BtnDelete.Enabled := TDataSource(Sender).State in [dsBrowse]; 
BtnAbort.Enabled := TDataSource(Sender).State in [dsEdit,dsInsert]; 
BtnSave.Enabled := TDataSource(Sender).State in
[dsEdit,dsInsert];end;三、让button按下后与DBNavigator一样
procedure TForm1.BtnAbortClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbcancel);end; procedure
TForm1.BtnAddClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbinsert);end; procedure
TForm1.BtnDeleteClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbdelete);end; procedure
TForm1.BtnEditClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbedit);end; procedure
TForm1.BtnFirstClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbfirst);end; procedure
TForm1.BtnLastClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nblast);end; procedure TForm1.BtnNextClick(Sender:
TObject);begin  dbnavigator1.BtnClick(nbnext);end; procedure
TForm1.BtnPriorClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbprior);end; procedure
TForm1.BtnSaveClick(Sender: TObject);begin 
dbnavigator1.BtnClick(nbpost);end;

自:

 

问:如何使dbgrid增加一列类似checkbox的控件,可以用鼠标来选择该行是否被选中,可以多选

   
最好的思路还是要在数据表里增加一个布尔字段,然后用DBCheckBox与它关联,而不是CheckBox。(至于在DBGrid里嵌入DBCheckBox的方法,可以参考:澳门新葡亰3522平台游戏,http://topic.csdn.net/t/20021118/16/1186480.html)

   
如果就是不希望在数据表里增加一个布尔字段,就是想用CheckBox,下面是我的测试代码供参考:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
  Dialogs, StdCtrls, ExtCtrls, DB, Grids, DBGrids, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;  
//这里用一个Table做测试。用其他的DataSet,如Query、ADOQuery等一样
    DBGrid1: TDBGrid;
    DataSource1:
TDataSource;//DataSource1如果换名字,下面的代码要随之更换
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect:
TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ChkOnClick(Sender: TObject); //注意
  end;

var
  Form1: TForm1;
  ChksState : array of boolean;    
//全程数组,存放每条记录的CheckBox状态

implementation