C# TreeView無窮目次樹完成辦法。本站提示廣大學習愛好者:(C# TreeView無窮目次樹完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C# TreeView無窮目次樹完成辦法正文
本文實例講述了C# TreeView無窮目次樹完成辦法。分享給年夜家供年夜家參考,詳細以下:
#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
願望本文所述對年夜家C#法式設計有所贊助。