Sorting child nodes of a treeview after populating the treeview in c# winforms

前端 未结 4 1312
谎友^
谎友^ 2020-12-11 20:36

I am having trouble sorting child nodes of a treeview in my winforms program. My treeview is populated by some XML files and it uses an internal text inside the xml files as

4条回答
  •  情歌与酒
    2020-12-11 21:22

    You need to create a custom comparer and assign it to the TreeViewNodeSorter property:

    public class NodeSorter : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            TreeNode tx = (TreeNode)x;
            TreeNode ty = (TreeNode)y;
    
            // Your sorting logic here... return -1 if tx < ty, 1 if tx > ty, 0 otherwise
            ...
        }
    }
    
    
    ...
    
    treeView.TreeViewNodeSorter = new NodeSorter();
    treeView.Sort();
    

提交回复
热议问题