PB 实现dddw联动

最近在自学PB,在网上找了很久,没有找到下拉数据窗口联动的例子,自己花了一下午的时间做了一个小例子,现在写出来给大家参考一下,望高手指点.

一、创建表
//用来存储区域信息
create table gb_area_info
(c_areano char(12) primary key,
c_areaname char(40),
c_fullname char(80,
i_usecount decimal(5,0),
c_other char(50),
c_input char(25),
c_hospital char(12))
//用户表
create table t_users
(userid char(10) primary key,
username char(20),
pwd char(30),
ctime datetime,
homeaddress)
二、创建数据窗口
1.创建  gb_area_info表数据窗口
按ctrl+n 选择dataWindow中的Grid,如下图

点击OK按钮,在choose data source for grid dataWindow窗口,选择SQLSelect数据源,
如下图

点击Next按钮,出现Select Tables窗口,选择表gb_area_info

点击Open按钮,出现如下界面

在aerainfo Type列中右击,选择Select All,选中所有的列,然后单击Return按钮,如图

点击Next按钮,然后点击Finish按钮,转到如下界面中

在菜单栏中选择Edit--Select--Select Columns或Select Text可以对列或文本进行设置,设置好之后,按Ctrl+S保存,将数据窗口的名字存为d_areainfo
2.创建  t_users表数据窗口
创建步骤同上,数据窗口的风格选择Freeform,选择表t_users,创建之后点击工具栏上的SQL,然后在Syntax中选择Syntax,再选择菜单栏中的Design---Convert To Syntax,在SQL语句中添加
left(homeaddress,2) as province,   
         left(homeaddress,4) as city,   
         left(homeaddress,6) as county,   
         left(homeaddress,8) as street,   
         left(homeaddress,10) as village,如下图

然后点击Return按钮返回,选中所有的Columns,将Border改为-Underline(4)
选中Province 列,在属性Tab中所选择Edit,将Style Type改为 Drop DownDW,选中Allow Editing和V ScrollBar,在DataWindow中选择d_areainfo数据窗口,Display Column中选择c_areaname,Data Column选择c_areano,city、county、street、village的属性和province一样,我就不再列举了。
homeaddress的属性Display Column 选择c_fullname,其它的都和上面的一样,修改好之后,
按Ctrl+S保存为d_area_test如下图

三、创建Window
按Ctrl+N,选择PB Object--Window,如图

然后在工具栏中拖一个数据窗口控件,或在菜单栏中选择Insert--Control--DataWindow,
选择DataWindowControl,在属性中的DataObject选择 d_area_test,再添加三个按钮,修改后保存为w_user_area,如图
然后在工具栏中拖一个数据窗口控件,或在菜单栏中选择Insert--Control--DataWindow,
选择DataWindowControl,在属性中的DataObject选择 d_area_test,再添加三个按钮,修改后保存为w_user_area,如图

四、在itemfocuschanged和itemchanged事件中添加代码
itemfocuschanged中的代码如下:
//声明子数据窗口
dataWindowChild province_childDataWind;
//市子数据窗口
dataWindowChild city_childDataWind;
//县子数据窗口
dataWindowChild county_childDataWind;
//地址子数据窗口
dataWindowChild homeaddress_childDataWind;

Integer flag;
string province_oldsql;
string province_newsql;
string city_oldsql;
string city_newsql;
string homeaddress_oldsql;
string homeaddress_newsql;
string province;
string city;
Integer index_where;
Integer index_from;
string where_cond;
string from_table;
Integer row_total;
Integer i;

choose case lower(getColumnName())
//如果列名是省份
case 'province'
//获取香省份下拉数据窗口的子数据窗口
flag=dw_1.getChild('province',province_childDataWind);
//获取子数据窗口的查询语句
province_oldsql=province_childDataWind.getSQLSelect();
//如果获取子数据窗口成功
if flag>0 then 
//设置子数据窗口的事务对象
province_childDataWind.setTransObject(sqlca);
//判断子数据窗口查询语句中是否有 where条件
index_where=pos(province_oldsql,'where');
//如果存在where 条件
if index_where>0 then
//将where条件取出
where_cond=right(province_oldsql,pos(province_oldsql,'where'));
//将 from 取出
from_table=left(province_oldsql,pos(province_oldsql,'FROM')+4);
province_oldsql=from_table+" gb_area_info ";
province_newsql=province_oldsql+" where len(c_areano)=2  ";
province_childDataWind.setSQLSelect(province_newsql);
province_childDataWind.retrieve();
else
index_from=pos(province_oldsql,'FROM');
province_oldsql=left(province_oldsql,index_from+4)+" gb_area_info ";
province_newsql=province_oldsql+" where len(c_areano)=2  ";
province_childDataWind.setSQLSelect(province_newsql);
province_childDataWind.retrieve();
end if
end if
case 'city'
flag=dw_1.getChild('city',city_childDataWind);
if flag>0 then
//设置事务对象
city_childDataWind.setTransObject(sqlca);
//获取查询语句
city_oldsql=city_childDataWind.getSqlSelect();
province=dw_1.getItemString(getRow(),'province');
index_where=pos(city_oldsql,'where1');
if (index_where>0) then
city_oldsql=left(city_oldsql,pos(city_oldsql,'where'));
if pos(city_oldsql,'w')>0 then
city_oldsql=left(city_oldsql,len(city_oldsql)-1);
end if
city_newsql=city_oldsql+" where left(c_areano,2)='"+province+"' and len(c_areano)>2 ";
city_childDataWind.setSqlSelect(city_newsql);
city_childDataWind.retrieve();
else
city_oldsql=left(city_oldsql,pos(city_oldsql,'FROM')+4)+" gb_area_info ";
city_newsql=city_oldsql+" where left(c_areano,2)='"+province+"' and len(c_areano)>2 ";
city_childDataWind.setSqlSelect(city_newsql);
city_childDataWind.retrieve();
end if
city_childDataWind.scrollToRow(city_childDataWind.getRow());
city=city_childDataWind.getItemString(city_childDataWind.getRow(),'c_areano');
city_childDataWind.selectRow(city_childDataWind.getRow(),true);
dw_1.setItem(getRow(),'homeaddress',city);
end if
case 'homeaddress'
//设置事务对象
homeaddress_childDataWind.setTransObject(sqlca);
homeaddress_childDataWind.retrieve();
city=dw_1.getItemString(getRow(),'city');
if (dw_1.getChild('homeaddress',homeaddress_childDataWind)=1) then
homeaddress_childDataWind.retrieve();
row_total=homeaddress_childDataWind.RowCount();
//遍历所有的行
for i=1 to row_total
if homeaddress_childDataWind.getItemString(i,'c_areano')=city then
homeaddress_childDataWind.scrollToRow(i);
homeaddress_childDataWind.selectRow(i,true);
end if
next
end if
end choose
itemchanged中有代码如下:
string city;
int r_num;
dataWindowChild city_childDataWind;

dw_1.getChild('city',city_childDataWind);
r_num=city_childDataWind.getRow();
choose case lower(getColumnName())
case 'city'
city=city_childDataWind.getItemString(r_num,'c_areano');
city_childDataWind.scrollToRow(r_num);
dw_1.setItem(getRow(),'homeaddress',city);
end choose
五、在Application中添加open(w_user_area),运行测试效果如下图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值