This is a migrated thread and some comments may be shown as answers.

treeview recursion

1 Answer 109 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
paul@red8.com
Top achievements
Rank 2
paul@red8.com asked on 25 Oct 2009, 05:06 AM
I am trying to do a simple recursion of folders based upon this folder structure:
Folder ID
Folder Name
Folder Parent

for example root folder has an id of 0 and parent is -1 identifying it as the root folder
Subsequent folders relate back to thier parent id.

Here is the code I got:
'Format Code Block'

 

Public Sub getparentfolders(ByVal parentid As String, ByVal name As String, ByVal rootnode As RadTreeNode)

 

DBConn =

New SqlConnection(ConfigurationManager.ConnectionStrings("db").ToString)

 

 

Dim roottable As New DataTable

 

 

Dim rootrow As DataRow

 

roottable = FolderAPI.GetAllFoldersByParent(parentid, DBConn)

 

Dim subtable As New DataTable

 

 

For Each rootrow In roottable.Rows

 

 

Dim ParentNode As New RadTreeNode

 

ParentNode.Text = rootrow(1).ToString

ParentNode.Value = rootrow(0).ToString

 

'Do I have sub folders

 

subtable = FolderAPI.GetAllFoldersByParent(rootrow(0).ToString, DBConn)

 

If subtable.Rows.Count = 0 Then

 

rootnode.Nodes.Add(ParentNode)

 

Else

 

name = rootrow(1).ToString

getchildfolder(rootrow(0).ToString, name, ParentNode, rootnode, childnode)

 

End If

 

 

Next

 

FolderList.Nodes.Add(rootnode)

 

End Sub

 

 

Public Sub getchildfolder(ByVal parentid As String, ByVal name As String, ByVal ParentNode As RadTreeNode, ByVal rootnode As RadTreeNode, ByVal Childnode As RadTreeNode)

 

 

If Childnode.Text = "" Then Childnode = ParentNode

 

DBConn =

New SqlConnection(ConfigurationManager.ConnectionStrings("db").ToString)

 

 

Dim roottable As New DataTable

 

 

Dim rootrow As DataRow

 

roottable = FolderAPI.GetAllFoldersByParent(parentid, DBConn)

 

If roottable.Rows.Count < 1 Then

 

 

Try

 

rootnode.Nodes.Add(ParentNode)

 

Catch ex As Exception

 

 

Dim parentnode2 As New RadTreeNode

 

parentnode2.Text = name

parentnode2.Value = parentid

 

Dim rootnode2 As New RadTreeNode

 

rootnode2.Text =

"Home"

 

rootnode2.Value = 0

rootnode = rootnode2

rootnode.Nodes.Add(parentnode2)

 

End Try

 

 

End If

 

 

Dim newchild As New RadTreeNode

 

 

For Each rootrow In roottable.Rows

 

 

'Do I have more folders

 

name = rootrow(1).ToString

newchild.Text = name

newchild.Value = parentid

Childnode.Nodes.Add(newchild)

 

'getparentfolders(parentid, name, ParentNode)

 

getchildfolder(rootrow(0).ToString, name, ParentNode, rootnode, newchild)

 

Next

 

 

End Sub

 

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

 

Dim rootnode As New RadTreeNode

 

rootnode.Value = -1

rootnode.Text =

"Root"

 

getparentfolders(

"0", "Root", rootnode)

 


Here is where it gets annoying, Works fine except for folders with two sub folders.
Then I get this:

Specified argument was out of the range of valid values.
Parameter name: index

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:

Line 36:             End If
Line 37:         Next
Line 38: FolderList.Nodes.Add(rootnode)

In debugging you can see the parent folder all of a sudden has an index of 2. WTF?

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 29 Oct 2009, 07:46 AM
Hi Paul,

Please check this help article which demonstrates how you can bind the treeview to a hierarchival data.

Regards,
Yana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
paul@red8.com
Top achievements
Rank 2
Answers by
Yana
Telerik team
Share this question
or