树视图混乱

本文关键字:混乱 视图 | 更新日期: 2023-09-27 18:33:50

基本上我有组列表,组,组和&&个人用户列表中的另一个用户列表。现在我想从用户列表中创建(不可扩展的)树节点,然后是组的另一个可扩展节点(其中包含用户列表),但我对树视图真的很糟糕,教程也没有帮助:/这是代码

            TreeView tree = new TreeView();
            List<string> GroupList = new List<string>();
            List<KeyValuePair<string, string>> UserList = new List<KeyValuePair<string, string>>();
            List<string> IndividualUserList = new List<string>();
            GroupList.Clear();
            UserList.Clear();
            IndividualUserList.Clear();
            foreach (string GroupListItem in GroupList)
            {
                TreeNode groupNode = new TreeNode();
                foreach (string Item in IndividualUserList)
                {
                    groupNode = new TreeNode(Item);
                }
                groupNode = new TreeNode(GroupListItem);
                foreach (KeyValuePair<string, string> UserPair in UserList)
                {
                    if (UserPair.Key == GroupListItem)
                    {
                        groupNode.ChildNodes.Add(new TreeNode(UserPair.Value));
                    }
                }
                tree.Nodes.Add(groupNode);
            }

树视图混乱

大致像这样的东西应该允许您在这些组中添加组和用户。

TreeView tree = new TreeView();
foreach(string groupName in Groups) 
{ 
    var groupNode = new TreeNode(groupName); 
    foreach(KeyValuePair user in UserList) 
    {
        if(user.Key == groupNode)
        {
            var userNode = new TreeNode(user);
            groupNode.Add(userNode);
        }
    }
    tree.Add(groupNode);
}