用TreeView构建树型目录

来源:流星之家



TreeView生成无限级分类的树型目录:
一、数据库设计


BBS_BestArticleFolder 精华区目录表

Key:   FolderID

Field name

Type

Length

Default

Description

FolderID

int

4

 

ID(自动编号)

ParentFolderID

int

4

0

父目录ID

FolderName

Nvarchar

80

 

目录名

BoardID

int

4

0

版块ID

CreaterUserID

int

4

0

创建者ID

AddTime

DateTime

8

GetDate()

创建时间


二、树型目录构建过程

1、安装TreeView控件,我想大多数人都是会安装的,不会安装的去找相关文章一下下。

2、具体实现:

a 数据生成

ContractedBlock.gif ExpandedBlockStart.gif   树的数据生成事件 #region 树的数据生成事件 
InBlock.gif
InBlock.gif         
private DataView CreateDateView(int boardID)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              BBSBestArticleFolderCollection bafc
=new BBSBestArticleFolderCollection();
InBlock.gif
InBlock.gif              bafc.GetInfoByParentFolderID(boardID,
-1);
InBlock.gif
InBlock.gif              
int bafcCount=bafc.Count;
InBlock.gif
InBlock.gif              DataTable dt 
= new DataTable("table");
InBlock.gif
InBlock.gif              dt.Columns.Add(
"FolderID",System.Type.GetType("System.String"));
InBlock.gif
InBlock.gif              dt.Columns.Add(
"FolderName",System.Type.GetType("System.String"));
InBlock.gif
InBlock.gif              dt.Columns.Add(
"ParentFolderID",System.Type.GetType("System.String"));
InBlock.gif
InBlock.gif              
for(int i=0;i<bafcCount;i++)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   DataRow dr
=dt.NewRow();
InBlock.gif
InBlock.gif                   dr[
0]=bafc[i].ID;
InBlock.gif
InBlock.gif                   dr[
1]=bafc[i].FolderName;
InBlock.gif
InBlock.gif                   dr[
2]=bafc[i].ParentFolderID;
InBlock.gif
InBlock.gif                   dt.Rows.Add(dr);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
InBlock.gif
InBlock.gif              dv
=dt.DefaultView;
InBlock.gif
InBlock.gif              
return dv;
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif         
#endregion

None.gif


一点说明:BBSBestArticleFolderCollection对象是个集合类,里面存储了所有符合条件的数据集合,GetInfoByParentFolderID是其中的一个方法,根据一定条件填充集合类的,此文主要讲TreeView构建树型目录,此处会在另外的文章中详述。

 

b

ContractedBlock.gif ExpandedBlockStart.gif TreeView节点生成方法 #region TreeView节点生成方法
InBlock.gif
InBlock.gif         
private void CreateTree(TreeNodeCollection folderTreeNodeCollection ,int parentID)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{                  dv.RowFilter= "ParentFolderID="+parentID;
InBlock.gif              
int tmpParentFolderID=0;
InBlock.gif
InBlock.gif              
foreach(DataRowView drv in  dv)
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif                   TreeNode tn
=new TreeNode();
InBlock.gif
InBlock.gif                   tn.ID
=drv.Row["FolderID"].ToString();
InBlock.gif
InBlock.gif                   tn.Text
=drv.Row["FolderName"].ToString().Trim();
InBlock.gif                   tmpParentFolderID
=Int32.Parse(drv.Row["ParentFolderID"].ToString().Trim());
InBlock.gif
InBlock.gif                   
//tn.CheckBox=true;
InBlock.gif

InBlock.gif                   folderTreeNodeCollection.Add(tn);                  
InBlock.gif
InBlock.gif                   CreateTree(folderTreeNodeCollection[folderTreeNodeCollection.Count 
- 1].Nodes,Int32.Parse(tn.ID));
ExpandedSubBlockEnd.gif              }

ExpandedSubBlockEnd.gif         }

InBlock.gif

 

一点解释:递归构建树的各级节点,其中的dv就是上一个方法生成的DataView

 

c  取得数据的方法有了,生节节点的方法也有了,那么剩下的就是调用了

 

                            CreateTree(TreeView1.Nodes,0);

 

一点解释:TreeView1是控件ID,这个大家都知道吧,这里的0其实就是表示顶级目录,从顶级目录递归调用下去DI。

转载于:https://www.cnblogs.com/wlq2000/archive/2005/07/08/188370.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值