Я пытаюсь заполнить дерево просмотра списком объектов.Элемент содержит имя и путь:
Вот код:
private void CreateTree(TreeNodeCollection nodeList, string path, string name)
{
TreeNode node = null;
string f = string.Empty;
int p = path.IndexOf('/');
if (p == -1)
{
f = path;
path = "";
}
else
{
f = path.Substring(0, p);
path = path.Substring(p + 1, path.Length - (p + 1));
}
node = null;
foreach (TreeNode item in nodeList)
{
if (item.Text == f)
{
node = item;
}
}
if (node == null)
{
node = new TreeNode(f);
nodeList.Add(node);
}
if (path != "")
{
CreateTree(node.Nodes, path, name);
}
}
Заполните список и создайте дерево:
List<Item> list = new List<Item>();
list.Add(new Item { Name = "Parent", Path = "1" });
list.Add(new Item { Name = "daughter", Path = "1/2" });
list.Add(new Item { Name = "son", Path = "1/3" });
list.Add(new Item { Name = "daughter", Path = "1/2/4" });
list.Add(new Item { Name = "daughter", Path = "1/2/5" });
list.Add(new Item { Name = "son", Path = "1/3/6" });
list.Add(new Item { Name = "son", Path = "1/3/7" });
list.Add(new Item { Name = "son", Path = "1/2/5/8" });
list.Add(new Item { Name = "daughter", Path = "1/2/5/9" });
list.Add(new Item { Name = "daughter", Path = "1/3/6/10" });
list.Add(new Item { Name = "son", Path = "1/3/6/11" });
list.Add(new Item { Name = "daughter", Path = "1/3/7/12" });
list.Add(new Item { Name = "daughter", Path = "1/3/7/13" });
list.Add(new Item { Name = "daughter", Path = "1/3/7/14" });
foreach (Item line in list)
{
CreateTree(treeView1.Nodes, line.Path, line.Name);
}
class Item
{
public string Name { get; set; }
public string Path { get; set; }
}
Это отлично работает, но вместо последнего числапуть я хочу показать имя.Как это сделать?