treeview recursion

2 posts, 0 answers
  1. paul@red8.com
    paul@red8.com avatar
    1 posts
    Member since:
    May 2007

    Posted 25 Oct 2009 Link to this post

    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?
  2. Yana
    Admin
    Yana avatar
    5013 posts

    Posted 29 Oct 2009 Link to this post

    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.
Back to Top