• TreeView(C#)无限目录树代码片段


        #region 绑定客户树
        protected void bindTreeView()
        {
            TreeView1.Nodes.Clear();
            string userid = Session["UserID"].ToString();
            string sqlwr = new SY_ADMINUSER().GetUserIDListByLoginUser(userid, "CUSTOMERSERVICE", false);
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query("Select PARENTID,CUSTOMERID,CUSTOMERSHORT,CUSTOMERSERVICE from CU_CUSTOMER where ISDELETE='0' " + sqlwr + " order by CUSTOMERSHORT ").Tables[0];
            //定义临时树节点  
            TreeNode tmpNd;
    
            //特殊处理
            foreach (DataRow dr in dt.Rows)
            {
                DataRow[] drs = dt.Select("CUSTOMERID='" + dr["PARENTID"] + "'");
                if (drs.Length == 0) //无上级节点
                {
                    dr["PARENTID"] = "0";
                }
            }
            dt.AcceptChanges();
    
            //从DataTable中得到所有父节点为null的DataRow形成的数组  
            DataRow[] rows = dt.Select("PARENTID='0'");
            //遍历根节点数组  
            foreach (DataRow row in rows)
            {
                tmpNd = new TreeNode();
                //给根节点赋值  
                tmpNd.Value = row["CUSTOMERID"].ToString();
                tmpNd.Text = row["CUSTOMERSHORT"].ToString();
                tmpNd.Expanded = true;
                //将节点加入到树中  
                TreeView1.Nodes.Add(tmpNd);
                AddTree(dt, TreeView1, tmpNd, row["CUSTOMERID"].ToString());
            }
        }
    
        public void AddTree(DataTable dt, TreeView tv, TreeNode pNode, string CUSTOMERID)
        {
            DataRow[] rows = dt.Select("PARENTID='" + CUSTOMERID + "'");
            TreeNode tmpNd;
            foreach (DataRow row in rows)
            {
                tmpNd = new TreeNode();
                //给根节点赋值  
                tmpNd.Value = row["CUSTOMERID"].ToString();
                tmpNd.Text = row["CUSTOMERSHORT"].ToString();
                tmpNd.Expanded = true;
                pNode.ChildNodes.Add(tmpNd);
                //将节点加入到树中  
                //TreeView1.Nodes.Add(pNode);
                AddTree(dt, TreeView1, tmpNd, row["CUSTOMERID"].ToString());
            }
        }
        #endregion


  • 相关阅读:
    MYSQL连接不上100061错误
    最小生成树
    装载问题
    贪心算法-- 纪念品分组
    折腾日记----「乱七八糟的过程」
    LeetCode ---- 474. 一和零「二维01背包」
    Erlang TCP 实例
    Erlang 入门 ---- 基础(一)
    SpringBoot ---- Spring Security
    LeetCode----1026. 节点与其祖先之间的最大差值
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3215148.html
Copyright © 2020-2023  润新知