利用数据库中的多级菜单表进行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 |