Hi Mark,
Thanks for the update.
The sample in the previous update demonstrates how the nodes can be sorted based on its subitems text, and obviously this will sort the nodes on the same level.
As you can see in your sample the parent nodes have a subitem with empty string and only a single node exists in its child level.
I have attached a new sample that demonstrates how the nodes on all the levels can be sorted based on the subitems text, and note this will sort nodes base on the subitems on the same level.
Have a close look at the third column "Parent Name" with child nodes expanded to see the sorting behavior.
VB
'----------------
SortNodesByColumn(Me.multiColumnTreeView1.Root, 1)
'----------------
Private Sub SortNodesByColumn(ByVal ParentNode As TreeNodeAdv, ByVal ColumnIndex As Integer)
For Each node As TreeNodeAdv In ParentNode.Nodes
If node.HasNodes Then
SortNodesByColumn(node, 1)
End If
node.Nodes.Sort(New SortBySubItems(Me.multiColumnTreeView1, ColumnIndex, Me.CheckBox1.Checked))
Next node
End Sub
'----------------
Public Class SortBySubItems
Implements IComparer
Private tree As MultiColumnTreeView
Private columnIndex As Integer
Private Ascending As Boolean
Public Sub New(ByVal tree As MultiColumnTreeView, ByVal columnIndex As Integer, ByVal Ascending As Boolean)
' Holds a reference to the Tree and ColumnIndex for sorting
Me.tree = tree
Me.columnIndex = columnIndex
Me.Ascending = Ascending
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim _nodeX As TreeNodeAdv = TryCast(x, TreeNodeAdv)
Dim _nodeY As TreeNodeAdv = TryCast(y, TreeNodeAdv)
If (Ascending) Then
Return _nodeX.SubItems(columnIndex).Text.CompareTo(_nodeY.SubItems(columnIndex).Text)
Else
Return _nodeY.SubItems(columnIndex).Text.CompareTo(_nodeX.SubItems(columnIndex).Text)
End If
End Function
End Class
'----------------
MA
Mark Attridge
October 18, 2010 03:04 PM UTC
Thanks for the newer code sample. I didn't think of using recursion for the sorting. However, this too does not quite get me what I am looking for.
Your sample does not follow the 2 main features in the sample I provided to you. #1) My sample does not have a single root node - it has 5. And #2) your sample does not take into account that the root nodes have blank entries in the subitem columns (as per my sample).
Maybe my requirements are very unique and the MultiColumnTreeView was not intended for sorting with this type of node setup. Unless you have a workable solution, I will have to resort to a custom way of sorting the nodes (without using the sorting capability of your MultiColumnTreeView control.