Click here to Skip to main content
11,634,827 members (68,012 online)
Click here to Skip to main content

A simple generic TreeList

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

Description

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
);
TreeControl1.SetDataSource(
	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);
TreeControl2.SetDataSource(
	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);
		tree.AddRange(getFolderTree(dir));
	}
	return tree;
}

License

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

Share

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 0:03
memberPranay Rana20-Aug-10 0:03 
GeneralReason for my vote of 5 Nice job~! Pin
Chen-Chung Chang18-Aug-10 16:22
memberChen-Chung Chang18-Aug-10 16: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.150728.1 | Last Updated 18 Aug 2010
Article Copyright 2010 by B. Verboven
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid