111,119
社区成员
发帖
与我相关
我的任务
分享

用无限递归就没问题,最近刚刚就是用这个方法实现的。
foreach( IDomainUser item in list )
{
int treeViewId = Convert.ToInt32( item.TreeViewId );
string branchName = string.Empty;
string branchImage = string.Empty;
if( item.GetType().Equals( typeof( DomainNameUI ) ) )
{
DomainNameUI data = ( DomainNameUI )item;
branchName = data.NameString;
branchImage = SetCreateConstants.Image_Url_Domain;
}
else if( item.GetType().Equals( typeof( OuUI ) ) )
{
OuUI data = ( OuUI )item;
branchName = data.NameString;
branchImage = SetCreateConstants.Image_Url_Ou;
}
else
{
return;
}
TreeComponent child = tree.CreateTreeBranch( treeViewId, branchName, null, branchImage );
parent.Add( child );
List<IDomainUser> ouList = item.GetOUList();
if( ouList != null && ouList.Count > 0 )
{
CreateTreeViewBranch( tree, child, ouList );
}
} /// <summary>
/// 实体类 客户信息树形视图展现
/// </summary>
public class ZBZone:BasicCheck,ICloneable
{
public ZBZone() { }
public object Clone()
{
return this.MemberwiseClone();
}
private string parentid;
/// <summary>
/// 父节点名称
/// </summary>
public string ParentID
{
get { return parentid; }
set {
if(value.ToString().IndexOf(',')!=-1)
parentid = value;
}
}
private string codeid;
/// <summary>
///用户id号
/// </summary>
public string CodeID
{
get { return codeid; }
set { codeid = value; }
}
private string codename;
/// <summary>
///用户名称
/// </summary>
public string CodeName
{
get { return codename; }
set { codename = value; }
}
} /// <summary>
/// 构造父节点树
/// </summary>
/// <param name="trc">树集合</param>
/// <param name="lzb">实体集合</param>
private void TreeViewLoad(TreeNodeCollection trc,List<ZBZone> lzb)
{
treeview_customer.Nodes.Clear();
foreach (ZBZone zb in lzb)
{
int icount = zb.ParentID.Split(',').Count();
if (icount == 2)
{
TreeNode tn = new TreeNode(zb.CodeName);
tn.Tag = zb;//设置tag属性
tn.ImageIndex = 0;
trc.Add(tn);
treeview_customer.Nodes[0].ExpandAll();
}
else
{
InitChildNode(trc[trc.Count - 1].Nodes,zb,zb.ParentID);
}
}
}
/// <summary>
/// 递归构造子节点
/// </summary>
/// <param name="trchild">父节点集合</param>
/// <param name="strparent">parentid长度</param>
/// <param name="name">节点名称</param>
private void InitChildNode(TreeNodeCollection trchild, ZBZone zbz,string parentid)
{
string stemp = parentid;
if (stemp.IndexOf(',') != 0)
{
stemp = stemp.Substring(0, stemp.LastIndexOf(','));
int icount = stemp.Split(',').Count();
if (icount ==2)
{
TreeNode tn1 = new TreeNode(zbz.CodeName);
tn1.Tag = zbz;
trchild.Add(tn1);
}
else
{
InitChildNode(trchild[trchild.Count - 1].Nodes, zbz, stemp);
}
}
}
//加载树 调用
List<ZBZone> zbzone = new List<ZBZone>();
zbzone.AddRange(new ZBZone[]
{new ZBZone{ ParentID="1,1" ,CodeID="1", CodeName="ONE"},
new ZBZone{ ParentID="1,1,1" ,CodeID="2", CodeName="ONE-1"},
new ZBZone{ ParentID="1,1,1,2" ,CodeID="5", CodeName="ONE-1-2"},
new ZBZone{ ParentID="2,2" ,CodeID="6", CodeName="TWO"}});
TreeViewLoad(treeview_customer.Nodes, zbzone);//树名称
01. //实现多级目录
02. //
03. public string rootFT_Id = "00";//根节点Tag
04. ///
05. /// 添加根节点
06. private void AddRootCompany()
07. {
08. DataSet ds = new DataSet();
09. using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=;Database=TW_KJ"))
10. {
11. con.Open();
12. string strSQL = "select * from TB_Personnel_Type where FT_ID ="+ rootFT_Id;
13. using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
14. {
15. adapter.Fill(ds);
16. }
17. TreeNode NewNode = new TreeNode();
18. NewNode.Text = ds.Tables[0].Rows[0]["FT_NAME"].ToString().Trim();
19. this.treeView1.Nodes.Add(NewNode);
20. InitTreeCompanyChildNode(NewNode,rootFT_Id);
21. }
22. }
23. public DataSet getMenuByLevel(string strFT_ID)
24. {
25. DataSet ds = new DataSet();
26. using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=;Database=TW_KJ"))
27. {
28. con.Open();
29. string strSQL;
30. if (strFT_ID == "00")
31. {
32. strSQL = "select * from TB_Personnel_Type where FT_ID like'" + "0_' and FT_UP_NO = 1";
33. }
34. else
35. {
36. strSQL = "select * from TB_Personnel_Type where FT_ID like'" + strFT_ID.Trim() + "__'";
37. }
38. using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
39. {
40. adapter.Fill(ds);
41. }
42. }
43. return ds;
44. }
45.
46. ///
47. /// 递归获取子节点
48. private void InitTreeCompanyChildNode(TreeNode pNode, string fatherFT_ID)
49. {
50. DataSet ds = getMenuByLevel(fatherFT_ID);
51. DataView dataView = new DataView();
52. dataView = ds.Tables[0].DefaultView;
53. foreach (DataRowView drv in dataView)
54. {
55. string newFT_ID = drv["FT_ID"].ToString();
56. string name = drv["FT_NAME"].ToString();
57. TreeNode NewNode = new TreeNode();
58. //将子节点添加到父节点下面
59. NewNode.Text = name;
60. pNode.Nodes.Add(NewNode);
61. InitTreeCompanyChildNode(NewNode,newFT_ID);
62. }
63. }
64.
65. private void Property_Load(object sender, EventArgs e)
66. {
67. AddRootCompany();
68.
69. }