原理就是先聲明常量,包括列數(shù),行數(shù),各列的屬性,然后在程序的其它過程用這些常量來控制Cells。非常方便,便于修改和移植!
以下為窗體整體代碼,中間有說明。此段代碼不光有動態(tài)AdvStringGrid的完美示例,還有一般窗體的常用的過程,比較窗體初始化,刷新過程。
此窗體,只需要簡單準(zhǔn)備如下,即可運(yùn)行:
1,添加一個TAdvStringGrid,并命名為strGrid1。
2,設(shè)置:TAdvStringGrid-->option-->goEditing=true
TAdvStringGrid-->enableGraphics=true
3,修改Form名稱為form1,或替換以下代碼中的form1為當(dāng)前窗體的名字。
4,將以下代碼覆蓋原來的代碼。
5,關(guān)聯(lián)以下過程(只需要在窗體和strGrid1控件屬性-事件頁中雙擊相關(guān)項即可完成關(guān)聯(lián)。)
FormCreate
FormShow
strGrid1CanEditCell
strGrid1GetAlignment
strGrid1GetCellColor
strGrid1GetEditorType
復(fù)制代碼 代碼如下:
unit ENMA0101;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, AdvGrid;
const
cUnit_ID='ENMA0101'; //聲明常量,保存單元名稱
//聲明常量,用于保存Cells的列數(shù)
cColQty1=8;
//聲明常量數(shù)組,保存Cells的各種屬性,各種屬性含義如下:
{0-顯示
1-編輯
2-必輸
3-類型
4-對齊
5-顏色
6-寬度
7-標(biāo)題
8-是否從數(shù)據(jù)庫中讀取
9-數(shù)據(jù)表名稱
10-字段名
11-字段長度
12-文本中讀取
13-文本中位置 }
cColProp1: array[0..cColQty1-1] of array[0..13] of string=( //列屬性
//顯示編輯必輸類型對齊 顏色 寬度 標(biāo)題
// 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}
//窗體創(chuàng)建時執(zhí)行代碼
procedure TForm1.FormCreate(Sender: TObject);
Var
i,j:integer;
begin
//設(shè)定行數(shù)最大值,用于設(shè)置CheckedBox
strGrid1.RowCount:=1000;
//設(shè)置cColProp1[3,J]='C'的單元格為CheckedBox格式
for i:=1 to strGrid1.rowcount-1 do begin
//通過以下屬性數(shù)組設(shè)置未通過,當(dāng)兩列checkbox時,只能設(shè)置一列。
{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;
//窗體顯示時執(zhí)行代碼
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;
//其它清空內(nèi)容
end;
//窗體刷新代碼
procedure Tform1.prRefresh(pMode: byte);
begin
//窗體刷新內(nèi)容
end;
//AdvStringGrid初始化過程
procedure TForm1.prStgInitialize1;
Var
I:Integer;
begin
//設(shè)定零件表初始行數(shù)和列數(shù)
strGrid1.RowCount:=2;
strGrid1.ColCount:=cColQty1;
strGrid1.FixedRows:=1;
strGrid1.FixedCols:=1;
//設(shè)定列寬度和列標(biāo)題
for I:=0 to cColQty1-1 do begin
strGrid1.Cells[I,0]:=cColProp1[I,7]; //標(biāo)題
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;
//設(shè)定單元表各列是否可以編輯
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}
//用屬性數(shù)組定義
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;
//以下代碼首先根據(jù)列cC1Data_Flag的值設(shè)定一行是否可以編輯,然后再根據(jù)屬性數(shù)組設(shè)定。
{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;
//設(shè)定單元表各列對齊方式
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; }
//用屬性數(shù)組定義
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;
//設(shè)定單元表各列顏色
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;}
//用屬性數(shù)組定義
if ARow=0 then
abrush.Color:=clBtnFace // 首行為灰色
else begin
for I:=0 to cColQty1 do begin // 非首行按屬性數(shù)組設(shè)置顏色
if ACol=I then abrush.Color:=StringToColor(cColProp1[I,5]);
end;
end;
end;
//設(shè)定單元表各列控件類型
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相關(guān)的屬性,只需要修改數(shù)組cColProp1相關(guān)的值就可以實(shí)現(xiàn)了,很方便的。
關(guān)于修改cColProp1,你可以直接在上面的代碼窗口中修改,但如果列相當(dāng)多的話,這樣改起來也是相當(dāng)麻煩的,而且容易改錯,也是不方便的!
對此,我做了一個excel模板:動態(tài)Cells設(shè)定工具表,如附件。
通過這個模板,可以自動生成靜態(tài)數(shù)組cColProp1和靜態(tài)列號,修改起來也很直觀,很方便。
修改后,將生成的靜態(tài)數(shù)組cColProp1和靜態(tài)列號復(fù)制到代碼里即可。
動態(tài)Cells設(shè)定工具表 http://xiazai.jb51.net/200906/other/DynamicCells_Setting.xls