Alex
Alex

Reputation: 509

Sort Treeview nodes, then insert node at (0)

I am having difficulty sorting Treeview nodes, then inserting a specific node at position 0.

For x 
    'Add nodes from database here
Next

tvwMain.Sort()

tvwMain.Nodes.Insert(0, "MainStepNode", "STEPS")

After running the code above, my MainStepNode "STEPS" gets sorted into the tree, when I specifically want this one node to be at position 0. Is there anyway in VB.NET to sort the nodes that you have, stop sorting, then add certain nodes at certain spots?

Upvotes: 1

Views: 234

Answers (1)

Ryan Roos
Ryan Roos

Reputation: 568

There is a 'Sorted' property that you can set to false after performing the sort. This will get you the results you want.

Results without setting the 'Sorted' property to false: Adam, Bob, James, Matt, Sam, Zack

Result after adding the 'Sorted' property assignment: Sam, Adam, Bob, James, Matt, Zack

   With Me.TreeView1.Nodes
        .Add("Bob")
        .Add("James")
        .Add("Adam")
        .Add("Zack")
        .Add("Matt")
    End With

    Me.TreeView1.Sort()
    Me.TreeView1.Sorted = False

    Me.TreeView1.Nodes.Insert(0, "Sam")

Upvotes: 2

Related Questions