By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,028 Members | 1,314 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,028 IT Pros & Developers. It's quick & easy.

sort treeview

P: n/a
Sam
Hi,
How can I sort the root nodes of a treeview according to a integer
stored in their tag property? I want to do that at run time on a
button's click event.

Thank you

Feb 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
AFAIK, the Sorted property of the Treeview only sorts alphabetically, so you
will have to set it to false and sort the treenodes in an array (Array.Sort)
before adding them to the treeview, providing an IComparer that uses the Tag
property of the treenodes.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Sam" <sa*************@googlemail.com> escribió en el mensaje
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
How can I sort the root nodes of a treeview according to a integer
stored in their tag property? I want to do that at run time on a
button's click event.

Thank you

Feb 9 '06 #2

P: n/a
Sam
Thanks for your answer Carlos.

Feb 9 '06 #3

P: n/a
A quick and dirty way, to avoid reload, could also be to use the sort
feature and to make the labels start with *formatted* numbers (if
acceptable) ...

-tom

Datatime Prj
http://cam70.sta.uniroma1.it/TechnicalPreview/

Feb 9 '06 #4

P: n/a
Sam
I've used the following to do it: ;-)

Public Class NodesList
Inherits List(Of TreeNode)

Public Overloads Sub Sort()
MyBase.Sort(New NodeTagComparer())
End Sub
End Class

Public Class NodeTagComparer
Implements IComparer(Of TreeNode)

Public Function Compare(ByVal node1 As TreeNode, ByVal node2 As
TreeNode) As Integer Implements IComparer(Of TreeNode).Compare
Return (DirectCast(node1.Tag,
Query).SortOrder.CompareTo(DirectCast(node2.Tag, Query).SortOrder))
End Function
End Class

Dim lstNodes As New AdminSystemLib.NodesList
For Each n As TreeNode In Me.Nodes
lstNodes.Add(n)
Next
lstNodes.Sort()

Feb 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.