We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to sort based on subitem columns in MultiColumnTreeView

I am using the MultiColumnTreeView control and trying to sort the information based on the selected column. I have 5 columns (main tree column plus 4 additional columns that hold the subitem information).

I have no problems sorting based on the main column by using the .Nodes.Sort method, but there is no similar .Sort method for the subitem columns (e.g. the .Columns(x) class -- there is a .Columns(x).SortOrder property, but no .Columns(x).Sort method).

Can anyone explain and/or provide a code sample of how I would sort based on the information in the subitem columns?


6 Replies

VS Vallarasu S Syncfusion Team October 15, 2010 05:52 AM UTC

Hi Mark,

Thanks for your interest in Syncfusion products.

Nodes on the MultiColumnTreeView can be sorted based on the subitems column by having a custom IComparer implementation as follows.



Private Sub SortNodesByColumn(ByVal ColumnIndex As Integer)
Me.multiColumnTreeView1.Nodes.Sort(New SortBySubItems(Me.multiColumnTreeView1, ColumnIndex))
End Sub


Public Class SortBySubItems
Implements IComparer
Private tree As MultiColumnTreeView
Private columnIndex As Integer

Public Sub New(ByVal tree As MultiColumnTreeView, ByVal columnIndex As Integer)
' Holds a reference to the Tree and ColumnIndex for sorting
Me.tree = tree
Me.columnIndex = columnIndex
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)

' Instead of comparing the nodes it compares the subitems based on the column index
Return _nodeX.SubItems(columnIndex).Text.CompareTo(_nodeY.SubItems(columnIndex).Text)
End Function
End Class


Refer the attached sample and let us know if you need further assistance.

Regards
Vallarasu S.




MultiColumnTreeView_Sort_VB_b355646f.zip


MA Mark Attridge October 15, 2010 03:51 PM UTC

Thanks for the sample code. I used this code in my project, but was unable to get the columns to sort properly.

I updated the issue in Direct-Trac and sent you back a updated sample project that demonstrates that sorting does not work.


Is there anyone else out there that has successfully implemented sorting on subitem columns? Any help would be appreciated...



MA Mark Attridge October 15, 2010 05:37 PM UTC

Also, as a helpful note for anyone trying to use this code. This code will always sort in "Ascending" order.

If you want to be able to toggle between sorting in Ascending/Descending order, then you need to update the SortBySubItem class to accept a "sort order" parameter and then handle the string comparison in the Compare method accordingly.

Example:

If sortOrder = SortOrder.Ascending then
'compare nodeX to nodeY
Return _nodeX.SubItems(columnIndex).Text.CompareTo(_nodeY.SubItems(columnIndex).Text)
Else
'compare nodeY to nodeX
Return _nodeY.SubItems(columnIndex).Text.CompareTo(_nodeX.SubItems(columnIndex).Text)
End If






VS Vallarasu S Syncfusion Team October 16, 2010 03:34 PM UTC

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
'----------------


Please let us know if you have concerns on this. We are happy to assist you.

Regards
Vallarasu S.



SortingSample_4b767b77.zip


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.




VS Vallarasu S Syncfusion Team October 20, 2010 11:55 AM UTC

Hi Mark,

Thanks for the update.

I have attached the modified sample at the following link,

http://www.syncfusion.com/uploads/redirect.aspx?&team=support&file=Sorting_VB_Sample-1145992802.zip

Have a look at the sample and please let us know your concerns.

Regards
Vallarasu S.



Loader.
Live Chat Icon For mobile
Up arrow icon