oracle

关注公众号 jb51net

关闭
首页 > 数据库 > oracle > oracle 动态AdvStringGrid

oracle 动态AdvStringGrid完美示例 (AdvStringGrid使用技巧/Cells)

作者:

本方法实现用常量和常量数组完美创建和控制动态TAdvStringGrid。
原理就是先声明常量,包括列数,行数,各列的属性,然后在程序的其它过程用这些常量来控制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
您可能感兴趣的文章:
阅读全文