不用递归的TreeView构建方法(优化方案)

 ID FatherID Title 0101 01 基本建设支出 010109 0101 其它基本建设支出

Private Function BuildENTree(ByVal ds As DataSet)

Dim rs As DataRowCollection

Dim r As DataRow

Dim ID As String

Dim FatherID As String

Dim Title As String

Dim fn As TreeNode

Dim node As TreeNode

rs = ds.Tables(0).Rows

BootNode = New TreeNode

BootNode.Text = "[0] 所有单位"

BootNode.Tag = "0"   '根目录

For Each r In rs

ID = DirectCast(r.Item(0), String).Trim   'CODE

FatherID = DirectCast(r.Item(1), String).Trim 'FATHERCODE

Title = "[" & ID & "] " & DirectCast(r.Item(2), String).Trim 'TITLE

'查找与父节点ID相同的节点

fn = FindNode(FatherID)

If fn Is Nothing Then

'没找到对应ID的节点

Else

node = New TreeNode

With node

.Tag = ID

.Text = Title

End With

End If

Next

BootNode.Expand()

End Function

'在指定的节点下查找ID相符节点

Private Function FindNode(ByVal n As TreeNode, ByVal ID As String) As TreeNode

Dim ns As TreeNodeCollection

Dim node As TreeNode

Dim Flag As Boolean

Dim strText As String

Dim ReturnNode As TreeNode

Flag = False

If n.Tag = ID Then

Return n

Else

'如果路径根本不相同则返回

If (ID.Length < DirectCast(n.Tag, String).Length) OrElse ((n.Tag <> "0") AndAlso (ID.Substring(0, n.Tag.Length) <> n.Tag)) Then

Return Nothing

End If

ns = n.Nodes

For Each node In ns

ReturnNode = FindNode(node, ID)

If ReturnNode Is Nothing Then

'do nothing

Else

Flag = True

Exit For

End If

Next

If Flag = True Then Return ReturnNode

End If

Return Nothing

End Function

Private FastHashTable As Hashtable

Private Function BuildENTree(ByVal ds As DataSet)

Dim rs As DataRowCollection

Dim r As DataRow

Dim ID As String

Dim FatherID As String

Dim Title As String

Dim fn As TreeNode

Dim node As TreeNode

rs = ds.Tables(0).Rows

BootNode = New TreeNode

BootNode.Text = "[0] 所有单位"

BootNode.Tag = "0"   '根目录

For Each r In rs

ID = DirectCast(r.Item(0), String).Trim   'CODE

FatherID = DirectCast(r.Item(1), String).Trim 'FATHERCODE

Title = "[" & ID & "] " & DirectCast(r.Item(2), String).Trim 'TITLE

'查找与父节点ID相同的节点

fn = FindNode(FatherID)

If fn Is Nothing Then

'没找到对应ID的节点

Else

node = New TreeNode

With node

.Tag = ID

.Text = Title

End With

End If

Next

BootNode.Expand()

End Function

'新版的快速查找

Private Function FindNode(ByVal ID As String) As TreeNode

Return FastHashTable.Item(ID)

End Function

FastHashTable = New Hashtable(3000) '数量

递归实现treeview应用的方法

2014-11-20 12:01:54

delphi TreeView 从数据库添加节点的四种方法

2017-10-27 12:24:06

C# TreeView 建立、遍历树（递归）

2016-12-07 15:59:00

TreeView递归添加节点

2015-09-21 10:59:13

ASP.NET树形控件TreeView的递归绑定

2013-01-24 15:25:00

Delphi 中递归生成TreeView节点的方法

2016-11-06 16:32:34

使用递归方法动态生成TreeView

2008年11月06日 2KB 下载

TreeView 递归添加子节点

2008-05-17 16:27:00

Treeview查找节点方法

2012-03-18 12:17:13

C# TreeView 节点查找

2011-10-19 10:57:09