递归地将节点添加到.NET TreeView中

时间:2013-09-27 09:23:37

标签: c# .net treeview

我需要创建一个从数据库表中获取的菜单结构,该数据库表使用ID和ParentID以及用于确定节点顺序的Rank。

Root(ID 1, ParentID 0, Rank 1)
  - Node(ID 2, ParentID 1, Rank 1)
    - Node(ID 3, ParentID 2, Rank 1)
      - Node(ID 4, ParentID 3, Rank 1)
      - Node(ID 5, ParentID 3, Rank 2)
    - Node(ID 6, ParentID 2, Rank 2)
    - Node(ID 7, ParentID 2, Rank 3)
  - Node(ID 8, ParentID 1, Rank 2)
    - Node(ID 9, ParentID 8, Rank 1)
    - Node(ID 10, ParentID 8, Rank 2)

我试图创建一个函数来迭代SQL数据并创建这个树结构,但我不确定如何处理添加的深度。我可以通过简单地检查它们是否具有ParentID来添加第一层节点,我可以在else条件中添加第二层节点,但是我不确定如何添加以下层的层次结构。

迭代数据库:

using (var command = new SqlCommand(_Query, _Connection))
{
    _Connection.Open();                
    var _Reader = command.ExecuteReader();
    while (_Reader.Read())
    {
        CreateNode((int)_Reader["MenuID"], (int)_Reader["ParentID"], (int)_Reader["Rank"], _Reader["English"].ToString());                    
    }
    _Connection.Close();
}

创建节点:

private void CreateNode(int id, int parentID, int rank, string text)
{            
    if(parentID == -1)
    {
        TreeNode _Node = new TreeNode(text, id.ToString());
        Root.Nodes.Add(_Node);
    }

    if (parentID != -1)
    {
        foreach (TreeNode _Node in Root.Nodes)
        {                    
            if (_Node.Value == parentID.ToString())
            {
                _Node.ChildNodes.Add(new TreeNode(text, id.ToString()) { ShowCheckBox = true } );
            }
        }
    }
}

目前,这不是按等级排序节点

我希望输出HTML类似于以下内容:

<ul id="1">
    <li>A</li>
    <li>
        <ul id="2">
            <li>B</li>
            <li>
                <ul id="3">
                    <li>C</li>
                    <li>
                        <ul id="4">
                            <li>D</li>
                        </ul>
                    </li>
                    <li>
                        <ul id="5">
                            <li>E</li>
                        </ul>
                    </li>
                </ul>                
            </li>
            <li>
                <ul id="6">
                    <li>F</li>
                </ul>
            </li>
            <li>
                <ul id="7">
                    <li>G</li>
                </ul>
            </li>            
        </ul>
    </li>        
    <li>
        <ul id="8">
            <li>H</li>
            <ul>
                <li>
                    <ul id="9">
                        <li>I</li>
                    </ul>
                </li>
                <li>
                    <ul id="10">
                        <li>J</li>
                    </ul>
                </li>                
            </ul>
        </ul>
    </li>
</ul>

http://jsfiddle.net/RjE7H/

3 个答案:

答案 0 :(得分:1)

使用字典,这样您就不必痛苦地扫描父树的树:

Dictionary<int, TreeNode> ParentCache = new Dictionary<int, TreeNode>();
private void CreateNode(int id, int parentID, int rank, string text)
{
    TreeNodeCollection parentNode = root.Nodes;
    if(parentID != 0)
    {
        TreeNode foundParentNode;
        if (!ParentCache.TryGetValue(parentID, out foundParentNode)
            throw new Exception("Given parentID has not been added to the tree yet - " + parentID.ToString());
        parentNode = foundParentNode.ChildNodes;
    }

    TreeNode newNode = new TreeNode(text, id.ToString());
    parentNode.Add(newNode);
    ParentCache.Add(id, newNode);
}

如果按照您指定的顺序接收数据,则输出应隐式按排名顺序排列。鉴于我们总是追加到TreeNodeCollections的末尾。

如果要忽略未找到节点父节点但仍想附加到根节点的任何异常,请进行以下更改:

    if(parentID != 0)
    {
        TreeNode foundParentNode;
        //Note: I changed the if logic from, "not TryGetValue" to "TryGetValue"
        if (ParentCache.TryGetValue(parentID, out foundParentNode)
            parentNode = foundParentNode.ChildNodes;
    }

答案 1 :(得分:0)

检查一下。这不是数据库驱动的,但可以帮助您构建层次树。

TreeView tv = new TreeView();
private void populateNode()
{

   for(int i=0;i<5;i++)
   {
      var parent = new TreeNode(i,string.Format("Node{0}",i));
      tv.Nodes.Add(parent);
      for(int j=0;j<=3;j++)
      {
        var child = new TreeNode(j,string.Format("childNode{0}",j)
        parent.ChildNodes.Add(child);
        for(int k=0;k<=3;k++)
        {
          var grandchild = new TreeNode(k,string.Format("grandchildNode{0}",k)
          child.ChildNodes.Add(grandchild);

        }
      }
   }

}

答案 2 :(得分:0)

你不能简单吗

  • 创建一些自定义对象的列表,这些对象具有您需要的数据库数据所需的所有属性(在阅读器循环中)。
  • 按Rank属性排序列表
  • 从排序列表中填充树视图?

不知怎的这样?

存储数据的一些结构

class TempTreeNode
{
    public int MenuID { get; set; }
    public int ParentID { get; set; }
    public int Rank { get; set; }
    public string Lang { get; set; }
}

生成列表的代码:

        var nodeList = new List<TempTreeNode>();

        using (var command = new SqlCommand(_Query, _Connection))
        {
            _Connection.Open();
            var _Reader = command.ExecuteReader();

            while (_Reader.Read())
            {
                var node = new TempTreeNode()
                {
                    MenuID = (int)_Reader["MenuID"],
                    ParentID = (int)_Reader["ParentID"],
                    Rank = (int)_Reader["Rank"],
                    Lang = _Reader["English"].ToString()
                };
                nodeList.Add(node);
            }
            _Connection.Close();
        }
        // sorting
        nodeList.Sort((a, b) => a.Rank.CompareTo(b.Rank));
        // creation
        CreateNodes(nodeList);

生成节点的方法......只是猜测你想要什么,所以这还不完整......

    private List<TreeNode> CreateNodes(List<TempTreeNode> nodes)
    {
        var rootNodes = new List<TreeNode>();
        foreach (var node in nodes)
        {
            if (node.ParentID == -1)
            {
                TreeNode _Node = new TreeNode(node.Lang, node.MenuID.ToString());
                rootNodes.Add(_Node);
            }
            [...] do whatever...
        }
        return rootNodes;
    }