oracle 动态AdvStringGrid完美示例 (AdvStringGrid使用技巧/Cells)
原理就是先声明常量,包括列数,行数,各列的属性,然后在程序的其它过程用这些常量来控制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
<div class="codetitle"><a style="CURSOR: pointer" data="80881" class="copybut" id="copybut80881" onclick="doCopy('code80881')"> 代码如下:
<div class="codebody" id="code80881">
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','E','R','clBtnFace','25','NO.','','0','0'),// 0
('Y','L','clInfoBk','150','Part No','Y','POJBSUB','PART_NO','30','2'),// 1
('Y','clWindow','60','Qty','ORDER_QUANTITY','9','3'),// 2
('Y','C','clMoneyGreen','40','U/M','UNIT_OF_MEASURE','2','4'),// 3
('Y','Dept','DELIVERY_TO_DEPT','3','5'),// 4
('Y','50','Grp','GROUP_A','7'),// 5
('Y','clSkyBlue','160','Part Name','PART_NAME_C','70','8'),// 6
('Y','M','DF','VENDOR_NO','5','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
//设定行数最大值,用于设置Checked
Box strGrid1.RowCount:=1000;
//设置cColProp1[3,J]='C'的单元格为Checked
Box格式
for i:=1 to strGrid1.rowcount-1 do begin
//通过以下
属性数组设置未通过,当两列check
Box时,只能设置一列。
{for j:=0 to cColQty1-1 do begin
if cColProp1[3,J]='C' then
strGrid1.AddCheck
Box(j,i,false,false);
end;}
//改为以下方式直接定义。
strGrid1.AddCheck
Box(4,false);
strGrid1.AddCheck
Box(5,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.SetCheck
BoxState(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]='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,248);
4: ABrush.Color:=RGB(250,193);
12: ABrush.Color:=RGB(227,248);
14: ABrush.Color:=RGB(250,193);
24: ABrush.Color:=RGB(227,248);
48: ABrush.Color:=RGB(250,193);
51: ABrush.Color:=RGB(227,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.