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

前端 未结 4 1310
谎友^
谎友^ 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:13

    Following is the solution I have used in my current project.

    public class NodeSorter : IComparer
    {
       public int Compare(object x, object y)
       {
          TreeNode tx = x as TreeNode;
          TreeNode ty = y as TreeNode;
          if (tx.Name== null || ty.Name== null)
             return 0;
          return (-1) * string.Compare(tx.Name.ToString(), ty.Name.ToString());
       }
    } 
    
    tvListofItems.TreeViewNodeSorter = new NodeSorter();
    tvListofItems.Sort();              
    

提交回复
热议问题