locked
how to save TreeView State

    Question

  • Hello all

    is it possible to save the state of a treeview??

    I may or delete items in treeview 

    later i want to restore the same with the state i saved before..

    thanks in advance.

    Tuesday, February 28, 2012 11:14 AM

Answers

  • Hello all

    is it possible to save the state of a treeview??

    Yes! In fact there is a nice handy code example that will show you exactly how this is done. Download it off the MSDN and walk through the code. It should have everything you need:

    Maintain ASP.NET TreeView State:
    http://code.msdn.microsoft.com/CSASPNETMaintainTreeViewSta-c7673683


    Thank you,

    • Proposed as answer by atconway Wednesday, February 29, 2012 3:21 PM
    • Marked as answer by Mike FengModerator Wednesday, March 07, 2012 12:31 PM
    Wednesday, February 29, 2012 3:20 PM
  • Hi Sadikvt,

    Welcome to the MSDN Forum.

    Does your application is asp.net one? If so, atconway's suggest should be helpful.

    If your application is a winform one, you can try to save your nodes into a XML file. And populate the treeview with your stored xml file node by the following code snippet:

        Private Sub AddTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlNode)
            For Each child_node As XmlNode In xml_node.ChildNodes
                Dim new_node As TreeNode = parent_nodes.Add(child_node.Name)
                'set your tag here
                If child_node.Attributes("Tag") IsNot Nothing Then
                    new_node.Tag = child_node.Attributes("Tag").Value
                Else
                    new_node.Tag = ""
                End If
    
                AddTreeViewChildNodes(new_node.Nodes, child_node)
                If new_node.Nodes.Count = 0 Then new_node.EnsureVisible()
            Next child_node
        End Sub
    
    
        Private Sub SaveTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlElement)
    
            For Each child_node As TreeNode In parent_nodes
                Dim new_node As XmlElement
                Dim doc As New XmlDocument
                new_node = doc.CreateElement(XmlNodeType.Element, child_node.Name, "")
                'set your tag here
                new_node.SetAttribute("Tag", child_node.Tag)
                xml_node.AppendChild(new_node)
    
                SaveTreeViewChildNodes(child_node.Nodes, new_node)
            Next child_node
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 01, 2012 6:27 AM

All replies

  • Hi sadikvt,

    check below sample to save TreeViewState

    http://blog.binaryocean.com/PermaLink,guid,23808645-43b5-4e2a-afb1-53dc8da35636.aspx 

    Regads

    Fazal

    Tuesday, February 28, 2012 5:13 PM
  • Hello all

    is it possible to save the state of a treeview??

    Yes! In fact there is a nice handy code example that will show you exactly how this is done. Download it off the MSDN and walk through the code. It should have everything you need:

    Maintain ASP.NET TreeView State:
    http://code.msdn.microsoft.com/CSASPNETMaintainTreeViewSta-c7673683


    Thank you,

    • Proposed as answer by atconway Wednesday, February 29, 2012 3:21 PM
    • Marked as answer by Mike FengModerator Wednesday, March 07, 2012 12:31 PM
    Wednesday, February 29, 2012 3:20 PM
  • Hi Sadikvt,

    Welcome to the MSDN Forum.

    Does your application is asp.net one? If so, atconway's suggest should be helpful.

    If your application is a winform one, you can try to save your nodes into a XML file. And populate the treeview with your stored xml file node by the following code snippet:

        Private Sub AddTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlNode)
            For Each child_node As XmlNode In xml_node.ChildNodes
                Dim new_node As TreeNode = parent_nodes.Add(child_node.Name)
                'set your tag here
                If child_node.Attributes("Tag") IsNot Nothing Then
                    new_node.Tag = child_node.Attributes("Tag").Value
                Else
                    new_node.Tag = ""
                End If
    
                AddTreeViewChildNodes(new_node.Nodes, child_node)
                If new_node.Nodes.Count = 0 Then new_node.EnsureVisible()
            Next child_node
        End Sub
    
    
        Private Sub SaveTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlElement)
    
            For Each child_node As TreeNode In parent_nodes
                Dim new_node As XmlElement
                Dim doc As New XmlDocument
                new_node = doc.CreateElement(XmlNodeType.Element, child_node.Name, "")
                'set your tag here
                new_node.SetAttribute("Tag", child_node.Tag)
                xml_node.AppendChild(new_node)
    
                SaveTreeViewChildNodes(child_node.Nodes, new_node)
            Next child_node
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 01, 2012 6:27 AM