代码之家  ›  专栏  ›  技术社区  ›  sara

以编程方式生成treeview节点

  •  1
  • sara  · 技术社区  · 16 年前

    如何以编程方式为此集合生成treeview节点

    node id          parent node id
    
    -------          --------------
    
    100                 null           //this is the root node
    
    101                 100
    
    123                 101
    
    124                 101
    
    126                 101
    
    103                 100
    
    104                 100
    
    109                 100
    
    128                 109
    
    122                 100
    
    127                 122
    
    129                 127
    
    130                 129
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   rodbv    16 年前

    这里有一些伪代码可以帮助您开始

    AddChildNodes(TreeNode parentNode)
    {
       var childNodeIds GetChildNodeIds(parentNode.Id);
       foreach (int childNodeId in childNodeIds)
       {
          TreeNode childNode = new TreeNode();
          //set other properties...
    
          //add to parent          
          parentNode.Nodes.Add(childNode);
    
          //call same function recursively
          AddChildNodes(childNode);
       } 
    

    }

    然后在程序中,首先获取所有不带父节点id(根节点)的项,为它们创建一个节点,然后调用上面的递归函数。