數(shù)據(jù)庫中樹形圖的用法
數(shù)據(jù)庫中樹形圖的用法的用法你知道嗎?下面小編就跟你們詳細(xì)介紹下數(shù)據(jù)庫中樹形圖的用法的用法,希望對你們有用。
數(shù)據(jù)庫中樹形圖的用法的用法如下:
樹形圖用于顯示按照樹形結(jié)構(gòu)進(jìn)行組織的數(shù)據(jù),其用途比較廣泛,如計(jì)算機(jī)中的文件系統(tǒng)(Windows中的資源管理器)、企業(yè)或公司的組成結(jié)構(gòu)等。我們知道在Windows下VB、PB、Delphi等工具提供了一個(gè)功能很強(qiáng)的樹型控件TreeView,利用Treeview控件可以方便地開發(fā)樹形圖。然而在網(wǎng)頁上實(shí)現(xiàn)樹形圖就不那么容易了,現(xiàn)在在asp.net中利用微軟提供的Internet Explorer WebControls它使得網(wǎng)頁上的樹形圖開發(fā)與在Windows下一樣的方便,一樣的功能強(qiáng)大,甚至更靈活。
本文介紹用Internet Explorer WebControls開發(fā)樹形圖的方法,由于樹形圖結(jié)構(gòu)較復(fù)雜,使用起來常不知如何下手。筆者結(jié)合最近剛為公司用ASP.NET編寫的應(yīng)用程序管理器這一具體實(shí)例,詳細(xì)闡述在ASP.NET下如何將Internet Explorer WebControls的使用與數(shù)據(jù)庫聯(lián)系起來,實(shí)現(xiàn)數(shù)據(jù)分任意多層顯示,方便地進(jìn)行增加、修改、刪除、移動(dòng)操作。筆者希望通過對該實(shí)例的闡述,達(dá)到拋磚引玉的效果,與各位同仁相互交流,共同進(jìn)步。
Internet Explorer WebControls不在VS.NET的標(biāo)準(zhǔn)Server Control中,要到微軟的站點(diǎn)上下載,下載地址是:
http://msdn.microsoft.com/downloads/samples/internet/default.asp?url=/Downloads/samples/Internet/ASP_DOT_NET_ServerControls/WebControls/default.asp
下載安裝后第一次使用時(shí),要右擊工具箱Customize Toolbox…→.NET Framework Components中找到Micosoft.Web.UI.WebControls.Treeview后選中,這樣Treeview控件就出現(xiàn)在工具箱中了。
一、樹的建立
具體方法是:創(chuàng)建一個(gè)數(shù)據(jù)庫,設(shè)計(jì)樹圖信息表TREE_INFO,包含NODEID、PARENTID、NODENAME、ADDErss、ICON字段,其它字段根據(jù)實(shí)際業(yè)務(wù)而定,節(jié)點(diǎn)名稱NODENAME將在樹型控件的節(jié)點(diǎn)上顯示,NODEID字段保存節(jié)點(diǎn)的唯一標(biāo)識(shí)號,PARENTID表示當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)號,標(biāo)識(shí)號組成了一個(gè)“鏈表”,記錄了樹上節(jié)點(diǎn)的結(jié)構(gòu)。設(shè)計(jì)一個(gè)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ǎn)開始遞歸調(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é)點(diǎn)
單純在Treeview 上增加、刪除、修改節(jié)點(diǎn)只需用Nodes屬性的Add、 Remove、等方法即可,值得注意的地方是VS.NET中Treeview的Nodes集合與VS6.0中的區(qū)別,VS6.0中的是一個(gè)大的集合,而VS.NET中的是分層的每個(gè)Node下都有Nodes屬性。增加、刪除、修改樹節(jié)點(diǎn)時(shí)與VS6.0相比有很大差別,特別是刪除時(shí)。
Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在選定的節(jié)點(diǎn)下添加子節(jié)點(diǎn)
Dim tmpNd As New TreeNode(), NdSel As TreeNode
tmpNd.ID = GetNewId()
NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’選中的節(jié)點(diǎn)
tmpNd.Text = "新節(jié)點(diǎn)"
NdSel.Nodes.Add(tmpNd)
Dim myRow As DataRow
myRow = ds.Tables("tree").NewRow()
myRow("NODE_NAME") = tmpNd.ID
myRow("NODE_DESCRipT") = "新節(jié)點(diǎn)" & 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é)點(diǎn)
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é)點(diǎn)的父節(jié)點(diǎn)的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
三、修改、移動(dòng)樹節(jié)點(diǎn)
由于服務(wù)器控件不支持鼠標(biāo)拖動(dòng)事件,所以不能象Windows程序那樣通過拖動(dòng)移動(dòng)節(jié)點(diǎn),這里是通過選擇父節(jié)點(diǎn)的方式。移動(dòng)是通過在原位置刪除,新位置添加實(shí)現(xiàn)的,要注意在刪除時(shí)先保存節(jié)點(diǎn)信息。
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
‘移動(dòng)節(jié)點(diǎn)
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é)點(diǎn)的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
‘由關(guān)鍵字查找節(jié)點(diǎn)
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