Private Sub frmDepManage_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim db As DataBase = New DataBase
Dim strSQL As String
strSQL = "select * from 组织机构编码表 order by absIndex"
dvList = db.RunSelectSQL(strSQL)
db.Dispose()
'初始化TreeView控件的各个节点
InitTree(trvList.Nodes, "-1")
End Sub
'InitTree是用递归方法初始化TreeView控件的节点
Private Sub InitTree(ByRef nodes As TreeNodeCollection, _
ByVal sParentIndex As String)
Try
Dim tmpNode As TreeNode
Dim absIndex As String
Dim dvTemp As DataView = dvList.Table.DefaultView
'选出数据源中ParentIndex为sParentIndex数据行
Dim dataRows() As DataRow = _
dvList.Table.Select("ParentIndex = '" + sParentIndex + "'")
'循环添加TreeNode
For Each dr As DataRow In dataRows
'获得节点所需数据
tmpNode = New TreeNode
tmpNode.Text = dr("类别")
'用TreeNode的Tag属性保存与此节点相关的数据
tmpNode.Tag = CType(New TreeNodeData(dr("AbsIndex"), _
dr("ItemIndex"), dr("ItemLevel"), _
dr("ParentIndex"), dr("类别号"), dr("单位编号")), Object)
absIndex = dr("AbsIndex")
'添加节点
nodes.Add(tmpNode)
'递归调用
InitTree(nodes(nodes.Count - 1).Nodes, absIndex)
Next
Catch ex As Exception
MessageBox.Show("初始化TreeView失败")
End Try
End Sub
这个方法可以子接点和根接点都用数据库里来调用!~你是这个意思吗?关键在于表的关系 要做好