侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

数据库  /  管理员 发布于 6年前   185

原理就是先声明常量,包括列数,行数,各列的属性,然后在程序的其它过程用这些常量来控制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..net.cn/200906/other/DynamicCells_Setting.xls


  • 上一条:
    oracle 集合
    下一条:
    Oracle 低权限数据库账户得到 OS 访问权限 提权利用
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客