These days in writing HRM This problem I two days, started in the use of Google to find a long time is a pile of garbage, are using more algorithms, and then went to the msdn.yesky.com find some inspiration. All right, nonsense, more useless.
First table structure follows table name Test
Write a stored procedure Gettreeview
I don't need to tell you this.
Cache DataTable for Speed
Public Function gettreetable () as DataTable Dim DT as New DataTable () DT = HttpContext.Current.Cache ("Treeview") If DT is nothing Then Dim Conn as New SqlConnection Dim Clsconndatabase as New connectiondatabase Conn = Clsconndatabase.conndatabase Dim Command as New SqlCommand Command.connection = Conn Command.commandtext = "Gettreeview" Command.commandtype = CommandType.StoredProcedure Command.executenonquery ()
Dim da as New SqlDataAdapter (Command)
DT = New DataTable () Da. Fill (DT) HttpContext.Current.Cache.Insert ("Treeview", DT) End If Return DT End Function
This is the main one.
Public Sub-Populatenodes (ByVal nodes as TreeNodeCollection, Optional ByVal intparentid as Int32 = 0)
Dim DT as New DataTable () DT = clswebforms.gettreetable () Dim strexpression as String strexpression = "[ParentID] =" & Intparentid Dim foundrows () as DataRow foundrows = dt. Select (strexpression) Dim I as Integer For I = 0 to Foundrows.getupperbound (0) Dim TN as New TreeNode () Tn. Text = FoundRows (I). Item ("tablename"). ToString () Tn. Value = FoundRows (I). Item ("ID"). ToString () Dim Dr () as DataRow Dr = dt. Select ("[ParentID] =" & TN. Value) If Dr. GetUpperBound (0) >-1 Then Tn. PopulateOnDemand = True End If Nodes. Add (TN) Next End Sub
Build WebForm into the TreeView
Protected Sub Page_Load (ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.load If not Page.IsPostBack Then Populatenodes (treeview1.nodes, 0) End If End Sub
Protected Sub treeview1_treenodepopulate (ByVal sender as Object, ByVal e as System.Web.UI.WebControls.TreeNodeEventArgs ) Handles Treeview1.treenodepopulate
Populatenodes (E.node.childnodes, E.node.value) End Sub |
As for speed I did not test, if you are interested to help test.