PB 使用Treeview调用数据库生成多级树

这篇博客介绍如何在PowerBuilder中利用数据库中的多级菜单表数据,通过TreeView控件生成数据树。作者展示了在窗体Open事件中插入和遍历数据的详细步骤,并给出了表结构创建和数据插入的SQL语句。
摘要由CSDN通过智能技术生成

利用数据库中的多级菜单表进行TreeView的数据树生成。

 

在窗体的Open()事件中,tv_1位TreeView的控件

 

string ls_code, ls_name, ls_lvcode, ls_plvcode
string ls_lastplvcode,ls_lastlvcode
treeviewitem ltvi_insert, ltvi_find
long ll_roothandle ,ll_lasthandle ,ll_parent,ll_level

tv_1.setredraw(false)

declare   c1   cursor   for
 select   distinct   at_code,at_name, at_lvcode,at_plvcode   from  dbo.asset_type   order  

 by   at_code  asc;
open   c1;  
fetch   c1   into   :ls_code, :ls_name, :ls_lvcode, :ls_plvcode;

ltvi_insert.label ="["+ ls_code+"]" + ls_name
ltvi_insert.data   =  ls_lvcode
ltvi_insert.pictureindex=1
ltvi_insert.selectedpictureindex=3
ltvi_insert.expanded= true
ll_roothandle=  tv_1.InsertItemLast(0,ltvi_insert)
ll_lasthandle = ll_roothandle

if isnull(ls_plvcode) then ls_plvcode = ""
ls_lastplvcode = ls_plvcode
ls_lastlvcode = ls_lvcode


labc1:  
 fetch   c1   into   :ls_code, :ls_name, :ls_lvcode, :ls_plvcode;  
 if isnull(ls_plvcode) then ls_plvcode = ""
 if   sqlca.sqlcode   =   0   then
  
  if ls_lastplvcode <> ls_plvcode  then
   if ls_lastlvcode  =  ls_plvcode then
    ll_level = ll_lasthandle
   else
    do      
     ll_parent = tv_1.finditem( ParentTreeItem! ,ll_lasthandle)
     if ll_parent = -1  then
      ll_level = ll_roothandle
      exit
     end if
     tv_1.getitem( ll_parent , ltvi_find )
     ll_level = ll_parent
     ll_lasthandle = ll_level
    loop until ltvi_find.data =ls_plvcode
   end if
  end if
  
  ltvi_insert.label ="["+ ls_code+"]" + ls_name
  ltvi_insert.data =  ls_lvcode
  ltvi_insert.pictureindex=1
  ltvi_insert.selectedpictureindex=3
  ltvi_insert.expanded= true
  ll_lasthandle=  tv_1.InsertItemLast(ll_level,ltvi_insert)
  
  ls_lastplvcode = ls_plvcode
  ls_lastlvcode = ls_lvcode
  goto   labc1
 end   if
close   c1;

tv_1.setredraw(true)  

 

/------------------------------------------------------------------------------------------------------/

表结构生成语句如下:(sql、sybase都可以)

create table dbo.asset_type (at_code varchar(20) not null, at_name varchar(50) null, at_lvcode varchar(20) not null, at_plvcode varchar(20) null, at_cjzlv decimal(12,2) null, at_zjtype smallint null, at_zjnx smallint null,  at_status varchar(1) default 0 null) ;
alter table dbo.asset_type add constraint at_pk primary key nonclustered (at_lvcode) ;
create nonclustered index idx_at_code on dbo.asset_type (at_code ) ;
create nonclustered index idx_at_plvcode on dbo.asset_type (at_plvcode ) ;

/------------------------------------------------------------------------------------------------------/

贴一段数据以便看到此文章的人进行调试

at_code at_name at_lvcode at_plvcode at_cjzlv at_zjtype at_zjnx at_status
01 房屋及构筑物 0001 00 120 1 0 0
0101 房屋 000101 0001 0 1 0 0
00 所有资产 00   0 0 0 0
03 仪器仪表 0003 00 0 1 0 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值