程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> TreeView綁定無限層級關系類

TreeView綁定無限層級關系類

編輯:C#入門知識

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind_TV(TreeView1.Nodes); } } #region 添加父節點 /// /// 添加父節點 /// /// private void Bind_TV(TreeNodeCollection tree) { //先添加第一級父節點 uint id = Convert.ToUInt32(Session["ID"]);//登陸人id string sqlmcount = "select * from account where status=1 and id='" + id + "'"; DataTable Trdtmcount = MySqlDbHelper.GetDataTable(sqlmcount, null); if (Trdtmcount.Rows.Count > 0) { TreeNode tn = new TreeNode(); tn.Value = Trdtmcount.Rows[0]["id"].ToString(); tn.Text = Trdtmcount.Rows[0]["nickname"].ToString(); tree.Add(tn); BindSon(Convert.ToUInt32(Trdtmcount.Rows[0]["id"]), tn.ChildNodes);//根據父節點添加子節點 } } #endregion   #region 遞歸添加子節點 /// /// 遞歸添加子節點 /// /// /// private void BindSon(uint fatherid, TreeNodeCollection tree) { string sqlMancount = "select id,nickname from account where masterid=" + fatherid + ";"; DataTable sonDT = MySqlDbHelper.GetDataTable(sqlMancount, null);   DataView dv = new DataView(sonDT); foreach (DataRowView item in dv) { //判斷此人id 是否有下屬 if (sonDT.Rows.Count > 0)//有 { TreeNode sontn = new TreeNode(); sontn.Value = item["id"].ToString(); sontn.Text = item["nickname"].ToString(); tree.Add(sontn); BindSon(Convert.ToUInt32(item["id"]), sontn.ChildNodes); } } } #endregion

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved