Click here to Skip to main content
13,353,095 members (73,588 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 18 Aug 2010

A simple generic TreeList

, 18 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Convert a generic list of (recursive) objects into a simple hierarchical TreeList


An easy way to convert a generic list into a hierarchical treelist.

Using the code

//A list of nested categories
List<Category> items = getItems();
TreeView<Category> tv = items.ToTreeView(
	c => c.Id, //Primary key
	c => c.ParentId //Foreign key
	tv, //Items
	c => c.Title, //DisplayMember
	c => "javascript:alert('" + c.Title + " (id: " + c.Id.ToString() + ")');" //ValueMember

//A list of nested directories
TreeList<string> tree = getFolderTree(foldername);
	tree, //Items
	s => s.Substring(s.LastIndexOf("\\") + 1), //DisplayMember
	s => "javascript:alert('" + Server.HtmlEncode(s).Replace(@"\", @"\\") + "');" //ValueMember

private TreeList<string> getFolderTree(string folder) {
	string[] folders = Directory.GetDirectories(folder);
	TreeList<string> tree = new TreeList<string>();
	foreach (string dir in folders) {
		tree.Add(dir, folder);
	return tree;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Verboven
Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 good tip Pin
Pranay Rana20-Aug-10 1:03
memberPranay Rana20-Aug-10 1:03 
GeneralReason for my vote of 5 Nice job~! Pin
Chen-Chung Chang18-Aug-10 17:22
memberChen-Chung Chang18-Aug-10 17:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 18 Aug 2010
Article Copyright 2010 by B. Verboven
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid