说实话一般不是你这样存储的,一般是用一个字段存储ID号,然后用另一个字段存储上级单位的ID号,然后用递归函数生成TreeView。建议你增加一个字段,然后将编号解析出的上级单位存储在里面。以下是我的一个程序用的递归函数:
表名是unit,其中包含了单位名称unit_name,编号unit_id,Unit_upper就是上级单位的id号。
调用方式是 addtree(ds1, TreeView1.Nodes, 0),其中ds1是一个DataSet,对应单位的表,treeview1是控件名称,0是固定值,与表中根节点部门的Unit_upper一致(这个单位没有上级)。
Public Function addtree(ByVal ds As DataSet, ByVal treename As TreeNodeCollection, ByVal x1 As Integer) As Boolean
Dim dt As DataTable = ds.Tables("unit")
Dim dm As BindingManagerBase = Me.BindingContext(ds, "unit")
Dim dr As DataRow() = dt.Select("unit_upper=" & x1.ToString)
Dim dr1 As DataRow
Dim nd As TreeNode
Dim nd1 As TreeNode
Dim x2 As Integer
If dr.GetLength(0) <> 0 Then
For Each dr1 In dr
nd = treename.Add(dr1("unit_name"))
nd.Tag = (dr1("unit_id"))
x2 = treename.IndexOf(nd)
addtree(ds, treename.Item(x2).Nodes, dr1("unit_id"))
Next
End If
Return True
End Function