why doesn't treeview provide application-defined sort?

  • Thread starter Thread starter Alan
  • Start date Start date
A

Alan

Hi all,
Besides the Sorted property, the TreeView control in .Net FCL doesn't
provide application-defined sort. But I put several kinds of data into the
treeview, and hope to sort them respectively. If setting its Sorted true,
all nodes will be sorted. It's not my requirement.

Any idea.

alan
 
Alan,

Have you tried the TreeViewNodeSorter property? It will allow you to
set an IComparer implementation used for the sort.

Hope this helps.
 
But which class has the TreeViewNodeSorter property? I don't find it. BTW, I
use the .Net FCL 1.1.

Thanks.

Nicholas Paldino said:
Alan,

Have you tried the TreeViewNodeSorter property? It will allow you to
set an IComparer implementation used for the sort.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alan said:
Hi all,
Besides the Sorted property, the TreeView control in .Net FCL doesn't
provide application-defined sort. But I put several kinds of data into the
treeview, and hope to sort them respectively. If setting its Sorted true,
all nodes will be sorted. It's not my requirement.

Any idea.

alan
 
Alan,

the easiest way to sort a TreeView is to:

* add the nodes to an ArrayList
* sort the ArrayList with an IComparer
* remove the nodes from the TreeView and add them back from the
ArrayList.

e.g. in a sub-class of TreeView:

public void Sort(IComparer comparer) {

ArrayList list = new ArrayList(this.Nodes.Count);
foreach (TreeNode childNode in this.Nodes) {
list.Add(childNode);
}
list.Sort(comparer);

this.BeginUpdate();
this.Nodes.Clear();
foreach (TreeNode childNode in list) {
this.Nodes.Add(childNode);
}
this.EndUpdate();
}

Obviously, this can be adapted to deal with the child-nodes of
individual nodes. Just pass a node to the method above, and replace
this.Nodes with node.Nodes.

You can also do it with API calls, but it's messy and as far as I can
tell, it's not much quicker. If you want to know this way, let me
know.

Chris.

Alan said:
But which class has the TreeViewNodeSorter property? I don't find it. BTW, I
use the .Net FCL 1.1.

Thanks.

Nicholas Paldino said:
Alan,

Have you tried the TreeViewNodeSorter property? It will allow you to
set an IComparer implementation used for the sort.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alan said:
Hi all,
Besides the Sorted property, the TreeView control in .Net FCL doesn't
provide application-defined sort. But I put several kinds of data into the
treeview, and hope to sort them respectively. If setting its Sorted true,
all nodes will be sorted. It's not my requirement.

Any idea.

alan
 
Back
Top