本文发表在 rolia.net 枫下论坛Imports System.Data.SqlClient
Public Class Class2
Public Sub AAA()
Dim myDS As DataSet = createTestDS()
Dim i As Integer
Dim strhtml As String = ""
Dim myName As String
Dim myArrayList As New ArrayList
For i = 0 To myDS.Tables(0).Rows.Count - 1
myName = CStr(myDS.Tables(0).Rows(i)("Parent"))
If HasParent(myName, myDS.Tables(0)) = False AndAlso _
Not myArrayList.Contains(myName) Then
myArrayList.Add(myName)
strhtml += Recursion(CStr(myDS.Tables(0).Rows(i)("Parent")), myDS.Tables(0))
End If
Next
MsgBox(strhtml)
End Sub
Private Function HasParent(ByVal name As String, ByVal DT As DataTable) As Boolean
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
If CStr(DT.Rows(i)("Child")) = name Then
Return True
End If
Next
Return False
End Function
Private Function Recursion(ByVal Name As String, ByVal DT As DataTable) As String
Dim StrStart As String = "<ul><li>"
Dim strEnd As String = "</li></ul>"
Dim returnStr As String = String.Format("{0}{1}", StrStart, Name)
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
If CStr(DT.Rows(i)("Parent")) = Name Then
returnStr += Recursion(CStr(DT.Rows(i)("Child")), DT)
End If
Next
Return returnStr + strEnd
End Function
Private Function createTestDS() As DataSet
Dim myDS As New DataSet
Dim myTB As New DataTable
Dim myCol As DataColumn
myCol = New DataColumn("Parent")
myTB.Columns.Add(myCol)
myCol = New DataColumn("Child")
myTB.Columns.Add(myCol)
myDS.Tables.Add(myTB)
Dim myDR As DataRow
'king(jones)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "jones"
myTB.Rows.Add(myDR)
'jones(scott)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "scott"
myTB.Rows.Add(myDR)
'scott(adams)
myDR = myTB.NewRow
myDR(0) = "scott"
myDR(1) = "adams"
myTB.Rows.Add(myDR)
'jones(ford)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "ford"
myTB.Rows.Add(myDR)
'ford(smith)
myDR = myTB.NewRow
myDR(0) = "ford"
myDR(1) = "smith"
myTB.Rows.Add(myDR)
'king(blake)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "blake"
myTB.Rows.Add(myDR)
'blake(allen)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "allen"
myTB.Rows.Add(myDR)
'blake(ward)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "ward"
myTB.Rows.Add(myDR)
'king(clark)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "clark"
myTB.Rows.Add(myDR)
'clark(miller)
myDR = myTB.NewRow
myDR(0) = "clark"
myDR(1) = "miller"
myTB.Rows.Add(myDR)
Return myDS
End Function
End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net
Public Class Class2
Public Sub AAA()
Dim myDS As DataSet = createTestDS()
Dim i As Integer
Dim strhtml As String = ""
Dim myName As String
Dim myArrayList As New ArrayList
For i = 0 To myDS.Tables(0).Rows.Count - 1
myName = CStr(myDS.Tables(0).Rows(i)("Parent"))
If HasParent(myName, myDS.Tables(0)) = False AndAlso _
Not myArrayList.Contains(myName) Then
myArrayList.Add(myName)
strhtml += Recursion(CStr(myDS.Tables(0).Rows(i)("Parent")), myDS.Tables(0))
End If
Next
MsgBox(strhtml)
End Sub
Private Function HasParent(ByVal name As String, ByVal DT As DataTable) As Boolean
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
If CStr(DT.Rows(i)("Child")) = name Then
Return True
End If
Next
Return False
End Function
Private Function Recursion(ByVal Name As String, ByVal DT As DataTable) As String
Dim StrStart As String = "<ul><li>"
Dim strEnd As String = "</li></ul>"
Dim returnStr As String = String.Format("{0}{1}", StrStart, Name)
Dim i As Integer
For i = 0 To DT.Rows.Count - 1
If CStr(DT.Rows(i)("Parent")) = Name Then
returnStr += Recursion(CStr(DT.Rows(i)("Child")), DT)
End If
Next
Return returnStr + strEnd
End Function
Private Function createTestDS() As DataSet
Dim myDS As New DataSet
Dim myTB As New DataTable
Dim myCol As DataColumn
myCol = New DataColumn("Parent")
myTB.Columns.Add(myCol)
myCol = New DataColumn("Child")
myTB.Columns.Add(myCol)
myDS.Tables.Add(myTB)
Dim myDR As DataRow
'king(jones)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "jones"
myTB.Rows.Add(myDR)
'jones(scott)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "scott"
myTB.Rows.Add(myDR)
'scott(adams)
myDR = myTB.NewRow
myDR(0) = "scott"
myDR(1) = "adams"
myTB.Rows.Add(myDR)
'jones(ford)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "ford"
myTB.Rows.Add(myDR)
'ford(smith)
myDR = myTB.NewRow
myDR(0) = "ford"
myDR(1) = "smith"
myTB.Rows.Add(myDR)
'king(blake)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "blake"
myTB.Rows.Add(myDR)
'blake(allen)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "allen"
myTB.Rows.Add(myDR)
'blake(ward)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "ward"
myTB.Rows.Add(myDR)
'king(clark)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "clark"
myTB.Rows.Add(myDR)
'clark(miller)
myDR = myTB.NewRow
myDR(0) = "clark"
myDR(1) = "miller"
myTB.Rows.Add(myDR)
Return myDS
End Function
End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net