自定义Treeview排序

发布于 2024-11-16 16:00:55 字数 489 浏览 2 评论 0原文

我有一个树视图,需要根据每个节点的标签以及 alpha beta 进行排序。

例如:

  • Node1 、 tag=A 、 text= Apple
  • Node2 、 tag=B , text= Baloon
  • Node3, tag=A, text= Help

我想对它进行排序,那个带有标签 A 的节点将首先出现,然后是带有标签 B 的节点。 但是,我希望包含标签 A 的节点从 A 到 Z 排序。

(顺序 = Node1,Node3,Node2

请帮助我, 我该怎么做?

提前致谢!

I have a treeview that need to be sorted according to the tag of every node and also according to the alpha beta.

for example:

  • Node1 , tag=A , text= Apple
  • Node2, tag=B , text= Baloon
  • Node3, tag=A, text= Help

I want to sort it, that nodes with tag A will be firsts, and just then nodes with tag B.
but, i want the nodes that contains tag A, to be sorted from A to Z.

(the order = Node1,Node3,Node2)

please help me ,
How can i do it?

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-11-23 16:00:55

假设您正在谈论 System.Windows.Forms.Treeview,您可以使用 TreeViewNodeSorter 和 IComparer 的实现来创建自定义排序策略。

http://msdn.microsoft.com/en -us/library/system.windows.forms.treeview.treeviewnodesorter.aspx

Assuming you're talking about System.Windows.Forms.Treeview, you can use TreeViewNodeSorter and an implementation of IComparer to create a custom sorting strategy.

http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.treeviewnodesorter.aspx

﹎☆浅夏丿初晴 2024-11-23 16:00:55

谢谢!我是这样做的:

 /// <summary>
        ///  Create a node sorter that implements the IComparer interface.
       /// </summary>
        public class NodeSorter : IComparer
        {
            // compare between two tree nodes
            public int Compare(object thisObj, object otherObj)
            {
                TreeNode thisNode = thisObj as TreeNode;
                TreeNode otherNode = otherObj as TreeNode;

                // Compare the types of the tags, returning the difference.
                if (thisNode.Tag is  first_type&& otherNode.Tag is another_type)
                    return 1;
                 //alphabetically sorting
                return thisNode.Text.CompareTo(otherNode.Text);
            }
        } 

thanks! I did it like that:

 /// <summary>
        ///  Create a node sorter that implements the IComparer interface.
       /// </summary>
        public class NodeSorter : IComparer
        {
            // compare between two tree nodes
            public int Compare(object thisObj, object otherObj)
            {
                TreeNode thisNode = thisObj as TreeNode;
                TreeNode otherNode = otherObj as TreeNode;

                // Compare the types of the tags, returning the difference.
                if (thisNode.Tag is  first_type&& otherNode.Tag is another_type)
                    return 1;
                 //alphabetically sorting
                return thisNode.Text.CompareTo(otherNode.Text);
            }
        } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文