Description
An easy way to convert a generic list into a hierarchical treelist.
Using the code
List<Category> items = getItems();
TreeView<Category> tv = items.ToTreeView(
c => c.Id,
c => c.ParentId
);
TreeControl1.SetDataSource(
tv,
c => c.Title,
c => "javascript:alert('" + c.Title + " (id: " + c.Id.ToString() + ")');"
);
TreeList<string> tree = getFolderTree(foldername);
TreeControl2.SetDataSource(
tree,
s => s.Substring(s.LastIndexOf("\\") + 1),
s => "javascript:alert('" + Server.HtmlEncode(s).Replace(@"\", @"\\") + "');"
);
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;
}