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

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
 

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

Comments and Discussions

 
GeneralReason for my vote of 5 good tip PinmemberPranay Rana20-Aug-10 0:03 
GeneralReason for my vote of 5 Nice job~! PinmemberChen-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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 18 Aug 2010
Article Copyright 2010 by B. Verboven
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid