Infinite depth hierarchal C1TreeView Binding

Posted by: kbj on 17 February 2023, 4:27 am EST

    • Post Options:
    • Link

    Posted 17 February 2023, 4:27 am EST

    Hi Folks,

    Is there a way to bind list of items that, themselves can contain a list of items (and so on) to a C1Treeview. It would seem that this would be a natural type of binding for a C1Treeview, similar to the way a file system is set up where a folder can contain folders which can contain folders . . .

    Here is a stripped down non bound example:

    Imports C1.Win.TreeView
    
    Public Class Form1
        Private lData As New List(Of classData)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            loadData()
            loadTreeView(C1TreeView1.Nodes, lData)
        End Sub
        Private Sub loadData()
            Dim parent1 = New classData("Parent1")
            Dim parent2 = New classData("Parent2")
            Dim parent3 = New classData("Parent3")
            Dim child1a = New classData("Child1a")
            Dim child1b = New classData("Child1b")
            Dim grandchild1b1 = New classData("Grandchild1b1")
            parent1.children.Add(child1a)
            parent1.children.Add(child1b)
            child1b.children.Add(grandchild1b1)
            lData.Add(parent1)
            lData.Add(parent2)
            lData.Add(parent3)
        End Sub
        Private Sub loadTreeView(nodes As C1TreeNodeCollection, l As List(Of classData))
            For Each item In l
                Dim node = New C1TreeNode(item)
                nodes.Add(node)
                loadTreeView(node.Nodes, item.children)
            Next
        End Sub
    
    
    End Class
    Public Class classData
        Public Property title As String
        Public Property children As New List(Of classData)
        Public Sub New(title As String)
            Me.title = title
        End Sub
        Public Overrides Function ToString() As String
            Return title
        End Function
    End Class
  • Posted 19 February 2023, 9:26 pm EST

    Hi,

    Thanks for reaching out to us.

    We are discussing this requirement with the development team. Will inform you once we have any updates.[Internal tracking Id - C1WIN-29152]

    Best Regards,

    Nitin

  • Posted 19 February 2023, 10:24 pm EST

    Hi,

    You can achieve this requirement by setting DataMenber as a collection of the Childs.(see code snippet)

    c1TreeView1.BindingInfo.DataSource = lData
    c1TreeView1.BindingInfo.DataMember = "children"

    Please refer the attached sample for the same:

    TreeViewDemo.zip

    Best Regards,

    Nitin

  • Posted 20 February 2023, 2:32 am EST - Updated 20 February 2023, 2:57 am EST

    Nitin,

    Thank you for your quick response. The solution works perfectly for lists as well as bindinglists (just make sure the children list is a bindinglist also)

    Kingman

  • Posted 13 December 2023, 2:26 am EST

    IMPORTANT NOTE

    The “children” list must be a property of the class, not a variable for this to work!

  • Posted 13 December 2023, 10:40 pm EST

    Hi,

    Apologize we didn’t get your point.

    Could you please elaborate in detail? Or please provide a small sample, where it is not working.

    Regards,

    Nitin

  • Posted 14 December 2023, 2:55 am EST

    This is just a reminder. If the “children” item in the classData class in the example above is a variable, not a property, the above example will not work. In other words, **c1TreeView1.BindingInfo.DataMember ** must be a property for child nodes to be propagated.

    Everything is working just fine.

    Thanks,

    Nitin

  • Posted 14 December 2023, 5:20 pm EST

    Hi,

    I have got your point.

    Yes, It should have the property for Child Node to work in this case.

    Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels