简体   繁体   English

如何使用字典填充Treeview

[英]How to populate treeview using Dictionary

Hi i am using dictionary to populate my treeview... 嗨,我正在使用字典填充我的树状视图...

Dictionary<int, Dictionary<string, int>> management = new Dictionary<int, Dictionary<string, int>>();

now management has the following values 现在管理层具有以下价值

ID  Name               Parent
1   Manager             0
2   Accountant          0
3   Assistant Manager   1
4   Branch Manager          1
5   Employee1           3
6   Employee3           3
7   Employee2           4
8   Accountant1         2
9   Accountant2             2

In the code behind DispalyTree() is the function that populate my treeview with dictionary vaules.. i know the below code is not complete.. i am just successful in displaying Manager and Accountant and i dont know how to populate its child nodes.. 在DispalyTree()背后的代码中是使用字典变量填充我的树视图的函数。.我知道以下代码并不完整..我只是成功显示了Manager和Accountant,但我不知道如何填充其子节点。

I have tried so much but not able to do.. i am stuck with this for like one week.. Please help 我已经尝试了很多,但是却做不到。.我坚持了大约一周。.请帮助

public void DisplayTree()
        {
                TreeNode parentnode = new TreeNode();
                TreeBL l_bltree = new TreeBL();
                Dictionary<int, Dictionary<string, int>> manage = new Dictionary<int, Dictionary<string, int>>();
                manage = l_bltree.ChildTree();
                foreach (var kvp in manage)
                {
                    var innerDict = kvp.Value;
                    foreach (var innerKvp in innerDict)
                    {
                        parentnode = new TreeNode(innerKvp.Key);
                        if (innerKvp.Value == 0)
                        {
                            treeview.Nodes.Add(parentnode);
                        }
                    }              
               }

        }

I think you're looking for the logic to build a tree view given a hierarchy. 我认为您正在寻找在给定层次结构的情况下构建树状视图的逻辑。

The values are present in the employees param. 这些值显示在employees参数中。 Build an Id->Employee map , iterate through the values, find parent and add the employee as parent 's child . 构建一个Id->Employee map ,遍历这些值,找到parent并将该employee添加parentchild At the end you only want the root or its children . 最后,您只需要root或其children

// quick and dirty class
public class Employee
{
    public int Id;
    public string Name;
    public int ParentId;
    public IList<Employee> Children = new List<Employee>();
    public override int GetHashCode()
    {
        return Id;
    }
    public override bool Equals(object obj)
    {
        Employee that;
        return
            obj != null
            && (that = obj as Employee) != null
            && Id == that.Id;
    }
}

// O(n) time, O(n) space for hashmap
public Employee BuildTreeView(IEnumerable<Employee> employees)
{
    var rootEmployee = new Employee() { Id = 0 };
    var IdToEmployeeMap = employees.ToDictionary(e => e.Id);
    IdToEmployeeMap[0] = rootEmployee;
    foreach (var employee in employees)
    {
        var parentEmployee = IdToEmployeeMap[employee.ParentId];
        parentEmployee.Children.Add(employee);
    }
    return rootEmployee; // or return rootEmployee.Children if interested in children
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM