Sort nodes from Tree view

Asked

Viewed 183 times

1

In my Tree view I have several child nodes. All nodes are created with numbers as these identify a different file and are composed by date.

How could I sort these nodes numerically? I mean, like:

Raiz
    1
    2
    3
    4

And so on and so forth...

Can anyone tell me if that’s possible?

  • Put an example of your code, so is very vague. Showing the structure can try to find some solution.

1 answer

2


Yes, it is possible.

You must create your own Comparer to and define it as property value TreeViewNodeSorter of TreeView

public class NodeSorter : System.Collections.IComparer
{
    public int Compare(object x, object y)
    {
        TreeNode treeNode1 = (TreeNode)x;
        TreeNode treeNode2 = (TreeNode)y;    

        string.Compare(treeNode1.Text, treeNode2.Text);
    }
}

minhaTreeView.TreeViewNodeSorter = new NodeSorter();

And when you need to rearrange the nodes

minhaTreeView.Sort();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.