原理就是先声明常量,包括列数,行数,各列的属性,然后在程序的其它过程用这些常量来控制cells。非常方便,便于修改和移植!
以下为窗体整体代码,中间有说明。此段代码不光有动态advstringgrid的完美示例,还有一般窗体的常用的过程,比较窗体初始化,刷新过程。
此窗体,只需要简单准备如下,即可运行:
1,添加一个tadvstringgrid,并命名为strgrid1。
2,设置:tadvstringgrid–>option–>goediting=true
tadvstringgrid–>enablegraphics=true
3,修改form名称为form1,或替换以下代码中的form1为当前窗体的名字。
4,将以下代码覆盖原来的代码。
5,关联以下过程(只需要在窗体和strgrid1控件属性-事件页中双击相关项即可完成关联。)
formcreate
formshow
strgrid1caneditcell
strgrid1getalignment
strgrid1getcellcolor
strgrid1geteditortype
复制代码 代码如下:
unit enma0101;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, grids, advgrid;
const
cunit_id=’enma0101′; //声明常量,保存单元名称
//声明常量,用于保存cells的列数
ccolqty1=8;
//声明常量数组,保存cells的各种属性,各种属性含义如下:
{0-显示
1-编辑
2-必输
3-类型
4-对齐
5-颜色
6-宽度
7-标题
8-是否从数据库中读取
9-数据表名称
10-字段名
11-字段长度
12-文本中读取
13-文本中位置 }
ccolprop1: array[0..ccolqty1-1] of array[0..13] of string=( //列属性
//显示编辑必输类型对齐 颜色 宽度 标题
// 0 1 2 3 4 5 6 7 8 9 10 11
(‘y’,’n’,’n’,’e’,’r’,’clbtnface’,’25’,’no.’,’n’,”,”,’0′,’n’,’0′), // 0
(‘y’,’n’,’n’,’e’,’l’,’clinfobk’,’150′,’part no’,’y’,’pojbsub’,’part_no’,’30’,’y’,’2′), // 1
(‘y’,’y’,’n’,’e’,’r’,’clwindow’,’60’,’qty’,’y’,’pojbsub’,’order_quantity’,’9′,’y’,’3′), // 2
(‘y’,’n’,’n’,’e’,’c’,’clmoneygreen’,’40’,’u/m’,’y’,’pojbsub’,’unit_of_measure’,’2′,’y’,’4′), // 3
(‘y’,’y’,’n’,’c’,’c’,’clwindow’,’60’,’dept’,’y’,’pojbsub’,’delivery_to_dept’,’3′,’y’,’5′), // 4
(‘y’,’y’,’n’,’c’,’c’,’clwindow’,’50’,’grp’,’y’,’pojbsub’,’group_a’,’3′,’y’,’7′), // 5
(‘y’,’n’,’n’,’e’,’l’,’clskyblue’,’160′,’part name’,’y’,’pojbsub’,’part_name_c’,’70’,’y’,’8′), // 6
(‘y’,’y’,’n’,’m’,’l’,’clwindow’,’50’,’df’,’y’,’pojbsub’,’vendor_no’,’5′,’y’,’6′) // 7
);
//声明常量,定义列号,便于修改与引用
cc1no=0;
cc1part_no=1;
cc1order_quantity=2;
cc1unit_of_measure=3;
cc1delivery_to_dept=4;
cc1group_a=5;
cc1part_name_c=6;
cc1data_flag=7;
type
tform1 = class(tform)
strgrid1: tadvstringgrid;
procedure formcreate(sender: tobject);
procedure formshow(sender: tobject);
procedure strgrid1caneditcell(sender: tobject; arow,
acol: integer; var canedit: boolean);
procedure strgrid1getalignment(sender: tobject; arow,
acol: integer; var aalignment: talignment);
procedure strgrid1getcellcolor(sender: tobject; arow,
acol: integer; astate: tgriddrawstate; abrush: tbrush; afont: tfont);
procedure strgrid1geteditortype(sender: tobject; acol,
arow: integer; var aeditor: teditortype);
private
{ private declarations }
procedure prclear(pmode:byte);
procedure prrefresh(pmode: byte);
procedure prstginitialize1;
procedure prstg1clear;
public
{ public declarations }
end;
var
form1: tform1;
implementation
{$r *.dfm}
//窗体创建时执行代码
procedure tform1.formcreate(sender: tobject);
var
i,j:integer;
begin
//设定行数最大值,用于设置checkedbox
strgrid1.rowcount:=1000;
//设置ccolprop1[3,j]=’c’的单元格为checkedbox格式
for i:=1 to strgrid1.rowcount-1 do begin
//通过以下属性数组设置未通过,当两列checkbox时,只能设置一列。
{for j:=0 to ccolqty1-1 do begin
if ccolprop1[3,j]=’c’ then
strgrid1.addcheckbox(j,i,false,false);
end;}
//改为以下方式直接定义。
strgrid1.addcheckbox(4,i,false,false);
strgrid1.addcheckbox(5,i,false,false);
end;
end;
//窗体显示时执行代码
procedure tform1.formshow(sender: tobject);
begin
form1.caption:= cunit_id + ‘ ‘ + form1.caption;
prclear(0);
end;
//窗体清空代码
procedure tform1.prclear(pmode:byte);
begin
case pmode of
0:begin
prstginitialize1;
end;
1:begin
prstg1clear;
end;
end;
//其它清空内容
end;
//窗体刷新代码
procedure tform1.prrefresh(pmode: byte);
begin
//窗体刷新内容
end;
//advstringgrid初始化过程
procedure tform1.prstginitialize1;
var
i:integer;
begin
//设定零件表初始行数和列数
strgrid1.rowcount:=2;
strgrid1.colcount:=ccolqty1;
strgrid1.fixedrows:=1;
strgrid1.fixedcols:=1;
//设定列宽度和列标题
for i:=0 to ccolqty1-1 do begin
strgrid1.cells[i,0]:=ccolprop1[i,7]; //标题
if ccolprop1[i,0]=’y’ then
strgrid1.colwidths[i]:=strtoint(ccolprop1[i,6]) //列宽
else
strgrid1.colwidths[i]:=0; //列宽
end;
end;
//advstringgrid清空过程
procedure tform1.prstg1clear;
var
i:integer;
j:integer;
begin
for i:=1 to strgrid1.rowcount-1 do begin
for j:=0 to ccolqty1-1 do begin
strgrid1.cells[j,i]:=”;
strgrid1.setcheckboxstate(j,i,false);
end;
end;
strgrid1.rowcount:=2;
end;
//设定单元表各列是否可以编辑
procedure tform1.strgrid1caneditcell(sender: tobject; arow,
acol: integer; var canedit: boolean);
var
i:integer;
begin
//直接定义
{if stgplist.cells[cncols1[3]-2,arow]=’1′ then
canedit:=false
else begin
if acol=cncols1[2]-2 then
canedit:=true
else
canedit:=false;
end;}
{if arow=0 then
canedit:=false
else if}
//用属性数组定义
for i:=0 to ccolqty1 do begin
if acol=i then begin
if ccolprop1[i,1]=’y’ then canedit:=true;
if ccolprop1[i,1]=’n’ then canedit:=false;
end;
end;
//以下代码首先根据列cc1data_flag的值设定一行是否可以编辑,然后再根据属性数组设定。
{if (strgrid1.cells[cc1data_flag,arow]=”) or (strgrid1.cells[cc1data_flag,arow]=’c’) then begin
canedit:=false;
exit;
end else begin
for i:=0 to ccolqty1 do begin
if acol=i then begin
if strgrid1.cells[cc1data_flag,arow]=’c’ then canedit:=false
else begin
if ccolprop1[i,1]=’y’ then canedit:=true;
if ccolprop1[i,1]=’n’ then canedit:=false;
end;
end;
end;
end;}
end;
//设定单元表各列对齐方式
procedure tform1.strgrid1getalignment(sender: tobject; arow, acol: integer; var aalignment: talignment);
var
i:integer;
begin
//直接定义
{if arow=0 then aalignment:=tacenter
else begin
case acol of
0: aalignment:=tarightjustify;
1: aalignment:=tacenter;
2: aalignment:=tacenter;
3: aalignment:=tarightjustify;
4: aalignment:=tacenter;
6: aalignment:=tacenter;
8: aalignment:=tacenter;
9: aalignment:=tacenter;
else aalignment:=taleftjustify;
end;
end; }
//用属性数组定义
if arow=0 then aalignment:=tacenter
else begin
for i:=0 to ccolqty1-1 do begin
if acol=i then begin
//case strtoint(ccolprop1[i,4])
if ccolprop1[i,4]=’c’ then aalignment:=tacenter;
if ccolprop1[i,4]=’l’ then aalignment:=taleftjustify;
if ccolprop1[i,4]=’r’ then aalignment:=tarightjustify;
end;
end;
end;
end;
//设定单元表各列颜色
procedure tform1.strgrid1getcellcolor(sender: tobject; arow,
acol: integer; astate: tgriddrawstate; abrush: tbrush; afont: tfont);
var
i:integer;
begin
//直接定义
{if arow>0 then begin
case acol of
1: abrush.color:=rgb(227,249,248);
2: abrush.color:=rgb(250,232,193);
3: abrush.color:=rgb(227,249,248);
4: abrush.color:=rgb(250,232,193);
12: abrush.color:=rgb(227,249,248);
14: abrush.color:=rgb(250,232,193);
24: abrush.color:=rgb(227,249,248);
48: abrush.color:=rgb(250,232,193);
51: abrush.color:=rgb(227,249,248);
end;
end;}
//用属性数组定义
if arow=0 then
abrush.color:=clbtnface // 首行为灰色
else begin
for i:=0 to ccolqty1 do begin // 非首行按属性数组设置颜色
if acol=i then abrush.color:=stringtocolor(ccolprop1[i,5]);
end;
end;
end;
//设定单元表各列控件类型
procedure tform1.strgrid1geteditortype(sender: tobject; acol,
arow: integer; var aeditor: teditortype);
var
i:integer;
begin
for i:=0 to ccolqty1 do begin
if acol=i then begin
if ccolprop1[i,3]=’m’ then begin
aeditor:=edcomboedit;
strgrid1.clearcombostring;
strgrid1.addcombostring(‘y : 同意’);
strgrid1.addcombostring(‘n : 拒绝’);
end;
end;
end;
end;
end.
(以上过程在delphi6中测试通过。)
这样,如果修改cells相关的属性,只需要修改数组ccolprop1相关的值就可以实现了,很方便的。
关于修改ccolprop1,你可以直接在上面的代码窗口中修改,但如果列相当多的话,这样改起来也是相当麻烦的,而且容易改错,也是不方便的!
对此,我做了一个excel模板:动态cells设定工具表,如附件。
通过这个模板,可以自动生成静态数组ccolprop1和静态列号,修改起来也很直观,很方便。
修改后,将生成的静态数组ccolprop1和静态列号复制到代码里即可。
动态cells设定工具表 http://xiazai.jb51.net/200906/other/dynamiccells_setting.xls