數(shù)據(jù)庫中樹形圖的用法
數(shù)據(jù)庫中樹形圖的用法的用法你知道嗎?下面小編就跟你們詳細介紹下數(shù)據(jù)庫中樹形圖的用法的用法,希望對你們有用。
數(shù)據(jù)庫中樹形圖的用法的用法如下:
樹形圖用于顯示按照樹形結構進行組織的數(shù)據(jù),其用途比較廣泛,如計算機中的文件系統(tǒng)(Windows中的資源管理器)、企業(yè)或公司的組成結構等。我們知道在Windows下VB、PB、Delphi等工具提供了一個功能很強的樹型控件TreeView,利用Treeview控件可以方便地開發(fā)樹形圖。然而在網(wǎng)頁上實現(xiàn)樹形圖就不那么容易了,現(xiàn)在在asp.net中利用微軟提供的Internet Explorer WebControls它使得網(wǎng)頁上的樹形圖開發(fā)與在Windows下一樣的方便,一樣的功能強大,甚至更靈活。
本文介紹用Internet Explorer WebControls開發(fā)樹形圖的方法,由于樹形圖結構較復雜,使用起來常不知如何下手。筆者結合最近剛為公司用ASP.NET編寫的應用程序管理器這一具體實例,詳細闡述在ASP.NET下如何將Internet Explorer WebControls的使用與數(shù)據(jù)庫聯(lián)系起來,實現(xiàn)數(shù)據(jù)分任意多層顯示,方便地進行增加、修改、刪除、移動操作。筆者希望通過對該實例的闡述,達到拋磚引玉的效果,與各位同仁相互交流,共同進步。
Internet Explorer WebControls不在VS.NET的標準Server Control中,要到微軟的站點上下載,下載地址是:
http://msdn.microsoft.com/downloads/samples/internet/default.asp?url=/Downloads/samples/Internet/ASP_DOT_NET_ServerControls/WebControls/default.asp
下載安裝后第一次使用時,要右擊工具箱Customize Toolbox…→.NET Framework Components中找到Micosoft.Web.UI.WebControls.Treeview后選中,這樣Treeview控件就出現(xiàn)在工具箱中了。
一、樹的建立
具體方法是:創(chuàng)建一個數(shù)據(jù)庫,設計樹圖信息表TREE_INFO,包含NODEID、PARENTID、NODENAME、ADDErss、ICON字段,其它字段根據(jù)實際業(yè)務而定,節(jié)點名稱NODENAME將在樹型控件的節(jié)點上顯示,NODEID字段保存節(jié)點的唯一標識號,PARENTID表示當前節(jié)點的父節(jié)點號,標識號組成了一個“鏈表”,記錄了樹上節(jié)點的結構。設計一個Web窗體其上放置TreeView控件。
PRivate Sub CreateDataSet()’建立數(shù)據(jù)集
Dim myConn As New SqlConnection()
Dim myCmd As New SqlCommand("select NODEID,NODENAME,PARENTID,ADDRESS,ICON from Tree_info", myConn)
Dim myDataAdapter As New SqlDataAdapter()
myConn.ConnectionString = application("connectstring")
myCmd.CommandText = ""
myCmd.Connection = myConn
myDataAdapter.SelectCommand = myCmd
myDataAdapter.Fill(ds, "tree")
End Sub
建樹的基本思路是:從根節(jié)點開始遞歸調(diào)用顯示子樹
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateDataSet()
intiTree(TreeView1.Nodes, 0)
End Sub
Private Sub intiTree(ByRef Nds As TreeNodeCollection, ByVal parentId As Integer)
Dim dv As New DataView()
Dim drv As DataRowView
Dim tmpNd As TreeNode
Dim intId As Integer
dv.Table = ds.Tables("tree")
dv.RowFilter = "PARENTID=’" & parentId & "’"
For Each drv In dv
tmpNd = New TreeNode()
strId = drv("NODE_ID")
tmpNd.ID = strId
tmpNd.Text = drv("NODE_NAME ")
tmpNd.ImageUrl = drv("ICON").ToString
Nds.Add(tmpNd)
intiTree(Nds(Nds.Count - 1).Nodes, intId)
Next
End Sub
二、增加、刪除樹節(jié)點
單純在Treeview 上增加、刪除、修改節(jié)點只需用Nodes屬性的Add、 Remove、等方法即可,值得注意的地方是VS.NET中Treeview的Nodes集合與VS6.0中的區(qū)別,VS6.0中的是一個大的集合,而VS.NET中的是分層的每個Node下都有Nodes屬性。增加、刪除、修改樹節(jié)點時與VS6.0相比有很大差別,特別是刪除時。
Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在選定的節(jié)點下添加子節(jié)點
Dim tmpNd As New TreeNode(), NdSel As TreeNode
tmpNd.ID = GetNewId()
NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’選中的節(jié)點
tmpNd.Text = "新節(jié)點"
NdSel.Nodes.Add(tmpNd)
Dim myRow As DataRow
myRow = ds.Tables("tree").NewRow()
myRow("NODE_NAME") = tmpNd.ID
myRow("NODE_DESCRipT") = "新節(jié)點" & tmpNd.ID & "_" & NdSel.ID
myRow("PARENT_NAME") = NdSel.ID
ds.Tables("tree").Rows.Add(myRow)
End Sub
Private Sub ButDele_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButDele.Click’刪除選中的節(jié)點
Dim idx As String = TreeView1.SelectedNodeIndex()
GetNdCol(idx).Remove(TreeView1.GetNodeFromIndex(idx))
Dim dv As New DataView(), recNo As Integer
dv.Table = ds.Tables("tree")
dv.RowFilter= "NODEID=" & NdId
dv.Delete(0)
End Sub
Private Function GetNdCol(ByVal idx As String) As TreeNodeCollection
‘獲得選中節(jié)點的父節(jié)點的Nodes集合
Dim cnt As Integer, i As Integer
Dim tmpNds As TreeNodeCollection
Dim idxs() As String
idxs = Split(idx, ".")
cnt = UBound(idxs)
If cnt = 0 Then
tmpNds = TreeView1.Nodes
Else
tmpNds = TreeView1.Nodes(CInt(idxs(0))).Nodes
For i = 1 To cnt - 1
tmpNds = tmpNds(CInt(idxs(i))).Nodes
Next
End If
Return tmpNds
End Function
三、修改、移動樹節(jié)點
由于服務器控件不支持鼠標拖動事件,所以不能象Windows程序那樣通過拖動移動節(jié)點,這里是通過選擇父節(jié)點的方式。移動是通過在原位置刪除,新位置添加實現(xiàn)的,要注意在刪除時先保存節(jié)點信息。
Private Sub TreeView1_SelectedIndexChange(ByVal sender As Object, ByVal e As Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs) Handles TreeView1.SelectedIndexChange
Dim dv As New DataView()
dv.Table = ds.Tables("tree")
Dim tmpNd As TreeNode = TreeNdSel(e.OldNode), tmpNds As TreeNodeCollection
dv.RowFilter= "NODEID=" & tmpNd.ID
dv(0)("NODE_DESCRIPT") = Me.TextBox1.Text
dv(0)("ADDRESS") = Me.TextBox2.Text
dv(0)("TARGET") = Me.TextBox3.Text
dv(0)("ICON") = Me.TextBox4.Text
If dv(0)("PARENTID").ToString <> Me.DropDownList1.SelectedItem.Value Then
‘移動節(jié)點
dv(0)("PARENT_NAME") = Me.DropDownList1.SelectedItem.Value
If Me.DropDownList1.SelectedItem.Value = "ROOT" Then
tmpNds = TreeView1.Nodes
Else
tmpNds = FromIdToNode(Me.DropDownList1.SelectedItem.Value, TreeView1.Nodes).Nodes’新的父節(jié)點的Nodes集合
End If
GetNdCol(e.OldNode).Remove(tmpNd)
tmpNds.Add(tmpNd)
End If
tmpNd.Text = Me.TextBox1.Text
tmpNd.ImageUrl = Me.TextBox4.Text
tmpNd = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)
dv.RowFilter= "NODEID=" & tmpNd.ID
Me.TextBox1.Text = dv(0)("NODENAME").ToString
Me.TextBox2.Text = dv(0)("ADDRESS").ToString
Me.TextBox3.Text = dv(0)("TARGET").ToString
Me.TextBox4.Text = dv(0)("ICON").ToString
End Sub
Private Function FromIdToNode(ByVal ID As String, ByVal Nds As TreeNodeCollection) As TreeNode
‘由關鍵字查找節(jié)點
Dim i As Integer
Dim tmpNd As TreeNode, tmpNd1 As TreeNode
For Each tmpNd In Nds
If tmpNd.ID = ID Then
Return tmpNd
Exit Function
End If
tmpNd1 = FromIdToNode(ID, tmpNd.Nodes)
If Not (tmpNd1 Is Nothing) Then
Return tmpNd1
Exit Function
End If
Next
Return Nothing
End Function