• C# 递归生产树 TreeView


     需要添加到TreeView 中的数据在数据库中的存储表: 

    ID 为主键,PID 表明数据之间的关系。

    /// <summary>
    /// 生产树的代码;
    /// </summary>
    /// <param name="node"> 根节点</param>
    /// <param name="id">主键</param>
    
            private void CreateTwo(TreeNode node, int id)
            {
                string strSql = "select * from TableTest where PID = " + id;
                DataTable dt = SqlClass.GetTable(strSql);
                if (id == 0)                                             // id = 0 是根节点 
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TreeNode nd = new TreeNode();
                        nd.Text = dt.Rows[i]["Name"].ToString();
                        CreateTwo(nd, Convert.ToInt32(dt.Rows[i]["id"].ToString()));
                        tvwTwo.Nodes.Add(nd);
                    }
                }
                else
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TreeNode Tnode = new TreeNode();
                        Tnode.Text = dt.Rows[i]["Name"].ToString();
                        CreateTwo(Tnode, Convert.ToInt32(dt.Rows[i]["id"].ToString()));
                        node.Nodes.Add(Tnode);
                    }
                }
            }
    
    
    则个代码比较简单 只需要查询一个数据表。
    

    生成结果:

      

  • 相关阅读:
    ASP的生成指定格式的GUID
    Principle
    Email icon generator
    Google 's Gmail
    防火墙
    注释
    对敏捷开发方法的一些疑问
    Faq about multimedia
    BSTR、char*和CString转换
    dshow配置环境vc6
  • 原文地址:https://www.cnblogs.com/igoogleyou/p/treeview2.html
Copyright © 2020-2023  润新知