private void GetTree() { DataTable dt = new DataTable(); var sql = @" select OUID,ParentOUID,OUCode,OUName,OUShortName from tbl_Base_OUInfo where LEN(oucode) in(10,15) order by LEN(oucode),OrderNo,ParentOUID"; dt = dataprovider.FillDataTable(sql, CommandType.Text); var list = DatatableToHash(dt); var r = new List<Hashtable>(); Hashtable root = new Hashtable(); //集团公司 root = list.Find(p => p["OUID"].ToString() == "CCB93E1A-CC22-4120-B99F-62E941CC6E4A".ToLower()); TreeData(list, root, "CCB93E1A-CC22-4120-B99F-62E941CC6E4A".ToLower()); r.Add(root); TreeNode root1 = new TreeNode(r[0]["OUName"].ToString()); root1.Name = r[0]["OUID"].ToString(); if (r[0].ContainsKey("children")) { var children = r[0]["children"] as List<Hashtable>; TreeBindData(root1, children); } this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] { root1 }); } private void TreeBindData(TreeNode node,List<Hashtable> list) { for(int i = 0; i < list.Count; i++) { if (list[i].ContainsKey("children")) { var children = list[i]["children"] as List<Hashtable>; TreeNode root1 = new TreeNode(list[i]["OUName"].ToString()); root1.Name = list[i]["OUID"].ToString(); TreeBindData(root1, children); node.Nodes.Add(root1); } else { TreeNode root1 = new TreeNode(list[i]["OUName"].ToString()); root1.Name = list[i]["OUID"].ToString(); node.Nodes.Add(root1); } } }
效果: