比如
id name superid indexid
1 aaa 0 1
2 bbb 0 2
3 ccc 0 3
4 aaa1 1 1
5 aaa2 1 2
6 aaa3 1 3
7 bbb1 2 1
8 bbb2 2 2
9 aaaa1 4 1
10 aaaa2 4 2其实需要的结果是
aaa
aaa1
aaaa1
aaaa2
aaa2
aaa3
bbb
bbb1
bbb2
ccc这样应该很明白了,用递归肯定是最方便的,因为是一级一级,可以有无限可下级,读取数据时,传递的参数也是一样的
id name superid indexid
1 aaa 0 1
2 bbb 0 2
3 ccc 0 3
4 aaa1 1 1
5 aaa2 1 2
6 aaa3 1 3
7 bbb1 2 1
8 bbb2 2 2
9 aaaa1 4 1
10 aaaa2 4 2其实需要的结果是
aaa
aaa1
aaaa1
aaaa2
aaa2
aaa3
bbb
bbb1
bbb2
ccc这样应该很明白了,用递归肯定是最方便的,因为是一级一级,可以有无限可下级,读取数据时,传递的参数也是一样的
Label1.Text = "<select name=field><option value=0>无分类</option>"
Label1.Text += getFcList("bm_tb", "bm_id", "部门", "上级部门", "0", mdataconnectstring)
Label1.Text += "</select>" Function getFcList(ByVal TableName As String, ByVal IDFiledName As String, ByVal ListFieldName As String, ByVal ParentFieldName As String, ByVal ParentID As String, ByVal DataConnectString As String) As String
Dim strcolor As String
Dim tmpcmd As New System.Data.SqlClient.SqlDataAdapter("select " & IDFiledName & "," & ListFieldName & "," & ParentFieldName & " from " & TableName & " order by " & ParentFieldName, DataConnectString)
Dim ds As New DataSet
tmpcmd.Fill(ds, "tmptable")
Dim tmptable As New DataTable
tmptable = ds.Tables("tmptable")
Dim tmpv As New DataView
tmpv.Table = tmptable
If ParentID <> "" Then
tmpv.RowFilter = ParentFieldName & "=" & System.Convert.ToInt32(ParentID)
End If
If FcList_ThisParentID = FcList_OldParentID Then
FcList_TmpStr = "├"
strcolor = "background-color:#cccccc"
Else
strcolor = "background-color:#f6f6f6"
End If
For Each drv As DataRowView In tmpv
FcList_ThisID = System.Convert.ToInt32(drv.Item(0))
FcList_ThisParentID = System.Convert.ToInt32(drv.Item(2))
If FcList_ThisParentID <> FcList_OldParentID And FcList_ThisParentID > 0 Then
FcList_TmpStr = FcList_TmpStr & "─"
If strcolor = "background-color:#cccccc" Then
strcolor = "background-color:#eeeeee"
End If End If
FcList_TmpStr11 += "<option value=" & FcList_ThisID & " style='" & strcolor & "'>" & FcList_TmpStr & System.Convert.ToString(drv.Item(1)) & "</option>"
getFcList(TableName, IDFiledName, ListFieldName, ParentFieldName, CStr(FcList_ThisID), mdataconnectstring)
FcList_OldParentID = FcList_ThisParentID
FcList_OldID = FcList_ThisID
Next
FcList_OldParentID = FcList_ThisParentID
getFcList += FcList_TmpStr11
End Function