RadControls version
|
All AJAX versions
|
.NET version |
v2.0 (I think) v3+
|
Visual Studio version |
Only tried in VS2k8
|
programming language |
C#
|
browser support |
all browsers supported by RadControls
|
PROJECT DESCRIPTION
I have, over the past couple of weeks, found myself writing the same, or similar code over and again. Particularly, I've been writing code to do searches of the RadTreeView starting a any given level, some times searching all of a node's dependants, sometimes, just the child nodes of that node.
Being basically a lazy guy, I decided to write a couple of extensions I could use in my programming without having to go through all the hassle of sub-classing this or that
telerik control class.
The following shows a couple of extensions that allow you to:
- do a generic search of all dependent nodes of a given node or treeview;
- sort the nodes collection of a treeview or tree node and, if required, apply that sort method recursively through all dependent nodes of the start point.
Let's look at the 'FindNodes' extension method first.
The signatures for the 2 FindNodes overloads look like this:
I have no doubt that the majority of readers can tell me more about generic types and methods applying to them than I could hope to know, so I'll keep this brief.
The first of these 2 overloads limits the search to the immediate children the treeview or node it is applied to, the second acts recursively through all the dependent child nodes of the starting point if the
Deep parameter is true.
Because the method use Predicate<RadTreeNode> the search criterion can be as complex as you need it to be. If the code is using C# v3 or later it can also be written using lambda notation. So, it is possible to search using code like this...
or, if you have something a little trickier ...
I'm sure you've work all this by now, so I'll shut up.
Next is the 'SortNodes' method which, if I'm honest, is based on a forum post by someone at
telerik towers.
Again, there are 2 overloads and, again, one operates on just the Nodes collection of the start node or treeview and the other operates recursively. The signatures look like this...
Once again, you can use anonymous delegates or lambda expressions for the Comparison<T>.
Incidentally, for those of you who haven't picked up on it, both methods described here can be used against RadTreeViews
or RadTreeNodes because the first argument is of type
IRadTreeNodeContainer.
So, I guess you want to see the code. If you have lots of projects that you use RadContols in, you might want to compile the extensions up in to a separate Library assembly that you can reuse.
Anyway, here's the code. I hope you find it useful.
Next time, I'll be showing a neat (I think) extension I use when using the treeview as a navigation control.
--
Stuart