Click here to Skip to main content
Click here to Skip to main content

A simple generic TreeList

, 18 Aug 2010 CPOL
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

Comments and Discussions

GeneralReason for my vote of 5 good tip PinmemberPranay Rana20-Aug-10 1:03 
GeneralReason for my vote of 5 Nice job~! PinmemberChen-Chung Chang18-Aug-10 17:22 

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

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

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