不知你用的 TreeView 和我用的是不是一样的。
我的程序中也用了TreeView,下面是VB.NET的源码:
Sub CreatRoot(ByVal treeView1 As Microsoft.Web.UI.WebControls.TreeView, ByVal BH As String, ByRef NodeCount As Integer)
'建根节点
Dim TreeNode As New Microsoft.Web.UI.WebControls.TreeNode
Dim sqlstr As String = "SELECT xm,BH,LJGZ FROM rmms WHERE BH='"
& BH & "' and NodeEmpty=true"
Dim LConn As New OleDbConnection(OleDBStr & Server.MapPath(MDB))
Dim myCommand As New OleDbCommand(sqlstr, LConn)
LConn.Open()
Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
Try
If myReader.HasRows then
While myReader.Read()
TreeNode.NodeData = myReader("BH"
.ToString
TreeNode.Text = myReader("XM"
.ToString & "
(编号:"
& myReader("BH"
.ToString & ",累计工资:"
& myReader("LJGZ"
.ToString & ")"
treeView1.Nodes.Clear()
treeView1.Nodes.Add(TreeNode)
End While
myReader.Close()
LConn.Close()
NodeCount = 1
CreatChildNode(TreeNode, BH, NodeCount)
End If
Finally
myReader.Close()
LConn.Close()
End Try
End Sub 'Main
Sub CreatChildNode(ByVal TreeNode As Microsoft.Web.UI.WebControls.TreeNode, ByVal HYBH As String, ByRef NodeCount As Integer)
'建子节点
Dim sqlstr As String = "SELECT xm,BH,LJGZ FROM rmms WHERE FBH='"
& HYBH & "' and NodeEmpty=true order by id "
Dim LConn As New OleDbConnection(OleDBStr & Server.MapPath(MDB))
Dim aHYBH() As String
Dim i As Integer
Dim myCommand As New OleDbCommand(sqlstr, LConn)
LConn.Open()
Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
Try
If myReader.HasRows then
i = 0
While myReader.Read()
Dim CTreeNode As New Microsoft.Web.UI.WebControls.TreeNode
CTreeNode.NodeData = myReader("BH"
.ToString
CTreeNode.Text = myReader("XM"
.ToString & "
(编号:"
& myReader("BH"
.ToString & ",累计工资:"
& myReader("LJGZ"
.ToString & ")"
TreeNode.Nodes.AddAt(i, CTreeNode)
i = i + 1
CreatChildNode(CTreeNode, myReader("BH"
.ToString, NodeCount)
NodeCount = NodeCount + 1
End While
myReader.Close()
LConn.Close()
End If
Finally
myReader.Close()
LConn.Close()
End Try
End Sub