在FORM上放一个dbcheck,
顾DBcheck.visible=false;
程序如下:
procedure TForm1.dbgriddrawcolumcell(..);
begin
if (dbfocused in state) and(column.field=table1senior) then
begin
dbcheckbox1.setbounds(rect.left+dbgrid1.left+1,
rect.top+dbgrid1.top+1,
rect.right-rect.left,
rect.bottom-rect.top);
end;
end;procedure TForm1.dbgrid1colenter(..);
begin
if dbgird1.columns[dbgrid1.selectedindex].field=tablesenior then
dbcheckbox1.visible:=true
else
dbcheckbox1.visible:=false;
end;
procedure TForm1.dbgird1keypress(..);
begin
if dbcheckbox1.visible and(ord(key)>31) then
begin
key:=#0;
table1.edit;
dbcheckbox1.checked:=not checkbox1.checked;
dbcheckbox1.field.asboolean:=dbcheckbox1.checked;
end;
end;
顾DBcheck.visible=false;
程序如下:
procedure TForm1.dbgriddrawcolumcell(..);
begin
if (dbfocused in state) and(column.field=table1senior) then
begin
dbcheckbox1.setbounds(rect.left+dbgrid1.left+1,
rect.top+dbgrid1.top+1,
rect.right-rect.left,
rect.bottom-rect.top);
end;
end;procedure TForm1.dbgrid1colenter(..);
begin
if dbgird1.columns[dbgrid1.selectedindex].field=tablesenior then
dbcheckbox1.visible:=true
else
dbcheckbox1.visible:=false;
end;
procedure TForm1.dbgird1keypress(..);
begin
if dbcheckbox1.visible and(ord(key)>31) then
begin
key:=#0;
table1.edit;
dbcheckbox1.checked:=not checkbox1.checked;
dbcheckbox1.field.asboolean:=dbcheckbox1.checked;
end;
end;
http://www.51delphi.com
//ondrawcolumncell
if (gdfocused in state) and (column.field=table1senior) then
begin
dbcheckbox1.setbounds(
rect.left+dbgrid1.left+1,
rect.top+dbgrid1.top+1,
rect.right-rect.left,
rect.bottom-rect.top);
end;//oncolenterif dbgrid1.columns[dbgrid.selectedindex].field=table1senior then
dbcheckbox1.visible:=true
else
dbcheckbox1.visible:=false;//onkeypress
if dbcheckbox1.visible and (ord(key)>31) then
begin
key:=#0;
table1.edit; //table1是对应的dataset
dbcheckbox1.checked:=not dbcheckbox1.checked;
dbcheckbox1.field.asboolean:= dbcheckbox1.checked;
end;
不过这种效果始终不能令人满意,假如你把checkbox所在column移到dbgrid的最右边,极有可能看到checkbox本身或超出dbgrid的边界;又或者你在dbgrid中新增一条记录,并点击checkbox所在column并使checkbox出现,然后把鼠标移到之前的记录,如果不提示保存出错的提示(可能你的表里没有做限制),那么你的checkbox就可能会停留在原地而与grid本身脱离。我说的这些,只是希望大家可以在重画这个处理上能够多花点心思,解决这些bug。
在解决这些问题之前,我觉得还是用第三方控件比较好,如infopower2000,如果需要,我可以mail给你——[email protected]