Public Class TreeOper
'找到后记录index值
Dim sFindIndex As String
'要查找的两个条件
Dim intFindID As Integer
Dim sFindNodeData As String
'找到标志
Dim bFind As Boolean
Dim strConn As String
Dim ds As New DataSet()
'根据某个节点的index值,让其变成选中节点,返回true表示index可以在tv中找到,返回false表示index值不在tv中
Public Function bMakeNodeSelected(ByVal tv As TreeView, ByVal ID As Integer, ByVal NodeData As String) As Boolean
Dim index As String
index = GetIndexFromIDandNodedata(tv, ID, NodeData)
If index = "" Then
bMakeNodeSelected = False
Exit Function
End If
Dim sTempIndex As String
Dim lastpos As Integer
sTempIndex = index
lastpos = sTempIndex.LastIndexOf(".")
Do While lastpos <> -1
tv.GetNodeFromIndex(sTempIndex).Expanded = True
'调用此函数要注意:
' 因为ds是全局变量,所以不能用同一个对象创建两棵树,必须为每棵树创建一个该类的对象。
'Public Sub CreateTree(ByVal tv As TreeView, ByVal OrgID As Integer, ByVal bLoadPerson As Boolean)
' '建立DataSet信息
' strConn = ConfigurationSettings.AppSettings("strConn")
' CreateDataSet(bLoadPerson)
' LoadTreeRoot(tv, OrgID, bLoadPerson)
' LoadTreeLeaves(tv.Nodes(0).Nodes, OrgID, bLoadPerson)
'End Sub
'*************************************************************************************************************************************
'#################################### Private Procedures #############################################################################
'*************************************************************************************************************************************
'根据ID和NodeData共同确定唯一的一个节点,得到他的index值
Private Function GetIndexFromIDandNodedata(ByVal tv As TreeView, ByVal ID As Integer, ByVal NodeData As String) As String
bFind = False
sFindIndex = ""
intFindID = ID
sFindNodeData = NodeData
RecurseTree(tv.Nodes)
GetIndexFromIDandNodedata = sFindIndex
End Function
Private Sub RecurseTree(ByVal ParentNode As TreeNodeCollection)
Dim node As TreeNode
For Each node In ParentNode
If bFind = True Then Exit Sub
If node.ID = intFindID And node.NodeData = sFindNodeData Then
bFind = True
sFindIndex = node.GetNodeIndex
Else
RecurseTree(node.Nodes)
End If
Next
End Sub
Private Sub CreateDataSet(ByVal bLoadPerson As Boolean) '在数据库中读出树形结构,填充到dataset中
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim objCmd As New SqlDataAdapter("select * from Org", objConn)
objCmd.Fill(ds, "Org")
If bLoadPerson Then
objCmd.SelectCommand.CommandText = "select * from Person"
objCmd.Fill(ds, "Person")
End If
objCmd.Dispose()
objConn.Close()
End Sub
'Private Sub LoadTreeRoot(ByVal tv As TreeView, ByVal OrgID As Integer, ByVal bLoadPerson As Boolean)
' Dim dboper As New DataOper()
' Dim sRootName As String
' Dim tnRoot As New TreeNode()
' '加载该部门下的人员
' If bLoadPerson = True Then
' LoadPersonToOrg(tnRoot)
' End If
'End Sub
Private Sub LoadTreeLeaves(ByRef Nds As TreeNodeCollection, ByVal ParentID As Integer, ByVal bLoadPerson As Boolean)
Dim dvOrg As New DataView()
Dim drvOrg As DataRowView
Dim tmpOrgNd As TreeNode
Dim intId As Integer
If bLoadPerson = True Then '如果该部门下面有人员的话,在此处加入目录树中
LoadPersonToOrg(tmpOrgNd)
End If
LoadTreeLeaves(tmpOrgNd.Nodes, intId, bLoadPerson) '加载子叶子
Next
End Sub
'如果该部门下面有人员的话,在此处加入目录树中
Private Sub LoadPersonToOrg(ByVal tnOrg As TreeNode)
Dim dvPerson As New DataView()
Dim drvPerson As DataRowView
Dim tmpPersonNd As TreeNode
dvPerson.Table = ds.Tables("Person")
dvPerson.RowFilter = "OrgID = " & tnOrg.ID
For Each drvPerson In dvPerson
tmpPersonNd = New TreeNode()
tmpPersonNd.ID = drvPerson("ID")
tmpPersonNd.NodeData = "0"
tmpPersonNd.Text = drvPerson("RealName")
tmpPersonNd.ImageUrl = "Images/Person.gif"
tnOrg.Nodes.Add(tmpPersonNd)
Next
End Sub
'用一句函数调用来完成一个节点的创造,主要是节省时间。
Public Function AddNode(ByRef ParentNodeCollection As TreeNodeCollection, ByVal Text As String, Optional ByVal Target As String = "", Optional ByVal NavigateUrl As String = "", Optional ByVal ImageUrl As String = "", Optional ByVal NodeData As String = "") As TreeNode
Dim tnTemp As TreeNode
tnTemp = New TreeNode
tnTemp.ID = Text
tnTemp.Text = Text
tnTemp.Target = Target
tnTemp.NodeData = NodeData
tnTemp.NavigateUrl = NavigateUrl
tnTemp.ImageUrl = ImageUrl