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

2016/12/29 15:38
阅读数 239
#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

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部