Dim parentId As Integer = 0
Dim list As New List(Of String)()
Dim rows = dt.Rows.Cast(Of DataRow)()
For Each item As var In rows
parentId = CInt(item("parentcategoryid"))
Dim name As String = item("Name").ToString()
While parentId > 0
Dim row = rows.Where(Function(n) CInt(n("Id")) = parentId).FirstOrDefault()
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid"))
name = String.Format("{0}->{1}", row("Name"), name)
End If
End While
list.Add(name)
Next
Dim result As DataRow() = dt.[Select]("Id = " & parentId)
if result.Length>0 Then
Dim row = result[0]
If row IsNot Nothing Then
parentId = CInt(row("parentcategoryid"))
name = String.Format("{0}->{1}", row("Name"), name)
End If
End If
Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
Dim curItem As String = listBox1.SelectedItem.ToString()
Dim list As New List(Of Integer)()
Dim tokens =curItem.Split(New String() {"->"}, StringSplitOptions.RemoveEmptyEntries)
For Each item As var In tokens
list.Add(Integer.Parse(item))
Next
if list.Count > 1 then
Dim parentCatId As Integer = list(list.Count - 2)
else
End Sub
dt -> the datatable that contains the data from DB
list -> contains the following list:
Books
Computers
Computers->Desktops
Computers->Notebooks
Computers->Desktops->Acces
Computers->Software
Computers->Games
Electronics
Electronics->Camera, photo
all you gotta do is bind the list to your dropdown list control.
cheers
Open in new window