首先,我们在SQL SERVER 2000里建立一个表tbTree,表的结构设计如下: 列名 数据类型 描述 长度 主键 ID Int 节点编号 4 是 ParentID Int 父节点编号 4 ConText Nvarchar 我们要显示的节点内容 50 在SQL SERVER 2000中建表的脚本:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ds As New DataSet() Dim CN As New SqlConnection() Try '初始化连接字符串 CN.ConnectionString = "data source=pmserver;initial catalog=Bench;persist security info=False;user id=sa;Password=sa;" CN.Open() Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from tbTree", CN) adp.Fill(ds) Me.ViewState("ds") = ds Catch ex As Exception #If DEBUG Then Session("Error") = ex.ToString() Response.Redirect("error.aspx") '̀跳转程序的公共错误处理页面 #End If Finally '关闭连接 CN.Close() End Try '调用递归函数,完成树形结构的生成 AddTree(0, Nothing) End Sub '递归添加树的节点 Private Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode) Dim ds As DataSet ds = Me.ViewState("ds") Dim dvTree As New DataView() dvTree = New DataView(ds.Tables(0)) '过滤ParentID,得到当前的所有子节点 dvTree.RowFilter = "PARENTID = " + ParentID.ToString Dim Row As DataRowView For Each Row In dvTree Dim Node As New TreeNode() If pNode Is Nothing Then '判断是否根节点 '添加根节点 Node.Text = Row("ConText").ToString() TreeView1.Nodes.Add(Node) Node.Expanded = True '再次递归 AddTree(Int32.Parse(Row("ID").ToString()), Node) Else '̀添加当前节点的子节点 Node.Text = Row("ConText").ToString() pNode.Nodes.Add(Node) Node.Expanded = True '再次递归 AddTree(Int32.Parse(Row("ID").ToString()), Node) End If Next End Sub
列名 数据类型 描述 长度 主键
ID Int 节点编号 4 是
ParentID Int 父节点编号 4
ConText Nvarchar 我们要显示的节点内容 50 在SQL SERVER 2000中建表的脚本:
CREATE TABLE [dbo].[tbTree] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ParentID] [int] NULL
) ON [PRIMARY]在表中添加如下记录:
SET IDENTITY_INSERT tbtree ON
insert tbtree (ID,Context,ParentID) values ( 1,'中国',0)
insert tbtree (ID,Context,ParentID) values ( 2,'北京',11)
insert tbtree (ID,Context,ParentID) values ( 3,'天津',11)
insert tbtree (ID,Context,ParentID) values ( 4,'河北省',1)
insert tbtree (ID,Context,ParentID) values ( 5,'广东省',1)
insert tbtree (ID,Context,ParentID) values ( 6,'广州',5)
insert tbtree (ID,Context,ParentID) values ( 7,'四川省',1)
insert tbtree (ID,Context,ParentID) values ( 8,'成都',7)
insert tbtree (ID,Context,ParentID) values ( 9,'深圳',5)
insert tbtree (ID,Context,ParentID) values ( 10,'石家庄',4)
insert tbtree (ID,Context,ParentID) values ( 11,'辽宁省',1)
insert tbtree (ID,Context,ParentID) values ( 12,'大连',11)
insert tbtree (ID,Context,ParentID) values ( 13,'上海',1)
insert tbtree (ID,Context,ParentID) values ( 14,'天河软件园',6)
insert tbtree (ID,Context,ParentID) values ( 15,'汕头',5)
SET IDENTITY_INSERT tbtree off
Dim ds As New DataSet()
Dim CN As New SqlConnection()
Try
'初始化连接字符串
CN.ConnectionString =
"data source=pmserver;initial catalog=Bench;persist security info=False;user id=sa;Password=sa;"
CN.Open()
Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from tbTree", CN)
adp.Fill(ds)
Me.ViewState("ds") = ds
Catch ex As Exception
#If DEBUG Then
Session("Error") = ex.ToString()
Response.Redirect("error.aspx") '̀跳转程序的公共错误处理页面
#End If
Finally
'关闭连接
CN.Close()
End Try
'调用递归函数,完成树形结构的生成
AddTree(0, Nothing)
End Sub '递归添加树的节点
Private Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode)
Dim ds As DataSet
ds = Me.ViewState("ds")
Dim dvTree As New DataView()
dvTree = New DataView(ds.Tables(0))
'过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "PARENTID = " + ParentID.ToString Dim Row As DataRowView
For Each Row In dvTree
Dim Node As New TreeNode()
If pNode Is Nothing Then '判断是否根节点
'添加根节点
Node.Text = Row("ConText").ToString()
TreeView1.Nodes.Add(Node)
Node.Expanded = True
'再次递归
AddTree(Int32.Parse(Row("ID").ToString()), Node)
Else
'̀添加当前节点的子节点
Node.Text = Row("ConText").ToString()
pNode.Nodes.Add(Node)
Node.Expanded = True
'再次递归
AddTree(Int32.Parse(Row("ID").ToString()), Node)
End If
Next
End Sub
http://community.csdn.net/Expert/topic/3657/3657310.xml?temp=.1241571