• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Question about sorting a treeview.

Hi,

I have a treeview with nodes with an imageindex 0 (folder node) and 1 (file node).
How can I sort the treeview first on the folder nodes and then the file nodes.
And sort them both alfabeticly. Like Windows Explores has.

Who knows the anwer and is willing to help me?

Greetings,

Peter Kiers
0
peterkiers
Asked:
peterkiers
  • 2
1 Solution
 
saraganiCommented:
Winforms?
0
 
saraganiCommented:
If so, then you can define your own sorting using IComparer.


        public class CompareTreeNodes : IComparer
        {
            public int Compare(object obj1, object obj2)
            {
                TreeNode treeNode1 = obj1 as TreeNode;
                TreeNode treeNode2 = obj2 as TreeNode;

                // Compare the length of the strings, returning the difference.
                if (treeNode1.ImageIndex != treeNode2.ImageIndex)
                    return treeNode1.ImageIndex - treeNode2.ImageIndex;

                // If they are the same length, call Compare.
                return string.Compare(treeNode1.Text, treeNode2.Text);
            }

        }




And in your code:
treeView1.TreeViewNodeSorter = new CompareTreeNodes();




You can read more about it here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.treeviewnodesorter.aspx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now