首页 > 代码库 > Treeview 排序

Treeview 排序

1. 继承 IComparer

2.  treeView1.TreeViewNodeSorter = this;

3. 实现IComparer

public int Compare(object x, object y)
        {
            TreeNode n1 = x as TreeNode;
            TreeNode n2 = y as TreeNode;
            if (n1 == null || n2 == null)
            {
                return 0;
            }

            IUser user1 = n1.Tag as IUser;
            IUser user2 = n2.Tag as IUser;
            if (user1 == null && user2 == null)
            {
                return n1.ToolTipText.CompareTo(n2.ToolTipText);
                //return n1.Text.CompareTo(n2.Text);
            }
            if (user1 != null && user2 != null)
            {
                return user1.Name.CompareTo(user2.Name);
            }

            return user1 == null ? 1 : -1;
        }

Treeview 排序