现在有四张表
1 //声明根节点 2 public void local() 3 { 4 TreeNode Node = new TreeNode(); 5 Node.Text = "部门"; 6 TreeView.Nodes.Add(Node); 7 Node.Expanded = true; //节点状态展开 8 bindType(Node); 9 } 10 public void bindType(TreeNode node) 11 { 12 string sql = "SELECT * FROM DepartmentType"; 13 DataTable dt = DBCommon.DbHelperSQL.Query(sql).Tables[0]; 14 DataView dv = new DataView(dt); 15 foreach (DataRowView Row in dv) 16 { 17 //声明节点 18 TreeNode Node = new TreeNode(); 19 Node.Text = Row["DepTypeName"].ToString(); 20 node.ChildNodes.Add(Node); 21 node.Expanded = true; //节点状态展开 22 Dep(Convert.ToInt32(Row["DepTypeID"]), Node); 23 } 24 } 25 public void Dep(int Pid,TreeNode node) 26 { 27 string sql = "SELECT * FROM Department where DepType=" + Pid + ""; 28 DataTable dt = DBCommon.DbHelperSQL.Query(sql).Tables[0]; 29 if (dt.Rows.Count > 0) 30 { 31 DataView dv = new DataView(dt); 32 foreach (DataRowView Row in dv) 33 { 34 //声明节点 35 TreeNode Node = new TreeNode(); 36 node.Text = Row["DepName"].ToString(); 37 node.ChildNodes.Add(Node); 38 node.Expanded = true; //节点状态展开 39 Banzu(Convert.ToInt32(Row["DepType"]), Convert.ToInt32(Row["DepID"]), Node); 40 if (Convert.ToInt32(Row["DepType"]) == 1) 41 { 42 post(1,Convert.ToInt32(Row["DepID"]), Node); 43 } 44 } 45 } 46 } 47 public void Banzu(int did,int Pid, TreeNode node) 48 { 49 50 //绑定班组 51 string sql = "SELECT GroupID,GroupName,BelongDep FROM Banzu where BelongDep=" + Pid + ""; 52 DataTable dt = DBCommon.DbHelperSQL.Query(sql).Tables[0]; 53 if (dt.Rows.Count > 0) 54 { 55 DataView dv = new DataView(dt); 56 foreach (DataRowView Row in dv) 57 { 58 //声明节点 59 TreeNode Node = new TreeNode(); 60 node.Text = Row["GroupName"].ToString(); 61 node.ChildNodes.Add(Node); 62 node.Expanded = true; //节点状态展开 63 post(2, Convert.ToInt32(Row["GroupID"]), Node); 64 } 65 } 66 } 67 public void post(int did, int Pid, TreeNode node) 68 { 69 if (did == 1) 70 { 71 //绑定岗位 72 string sql = "SELECT * FROM Post where BelongDep=" + Pid + ""; 73 DataTable dt = DBCommon.DbHelperSQL.Query(sql).Tables[0]; 74 if (dt.Rows.Count > 0) 75 { 76 DataView dv = new DataView(dt); 77 foreach (DataRowView Row in dv) 78 { 79 //声明节点 80 TreeNode Node = new TreeNode(); 81 node.Text = Row["PostName"].ToString(); 82 node.ChildNodes.Add(Node); 83 node.Expanded = true; //节点状态展开 84 } 85 } 86 } 87 if (did == 2) 88 { 89 //绑定岗位 90 string sql = "SELECT * FROM Post where BelongGroup=" + Pid + ""; 91 DataTable dt = DBCommon.DbHelperSQL.Query(sql).Tables[0]; 92 if (dt.Rows.Count > 0) 93 { 94 DataView dv = new DataView(dt); 95 foreach (DataRowView Row in dv) 96 { 97 //声明节点 98 TreeNode Node = new TreeNode(); 99 node.Text = Row["PostName"].ToString(); 100 node.ChildNodes.Add(Node); 101 node.Expanded = true; //节点状态展开 102 } 103 } 104 } 105 }
用以上代码 绑定后出现的是这个结果
少了部门类型,不知道哪里出了问题
还有,最后一个节点的 “-”怎么去掉?
4个表的数据全查出来,按照level逻辑组装node不就好了么
按照顺序 填充node就行了