代码
private UmDept[] entitys;
private void Form1_Load(object sender, EventArgs e)
{
this.bindtree("0"); //无限级分类
}
/// <summary>
/// 查找根节点(parent_ID为0的节点)的子节点
/// </summary>
/// <param name="parent_ID">参数,接收根节点ID</param>
private void bindtree(string parent_ID)
{
entitys = wsProxy.getAllDept();//获取所有树形数据
TreeNode node = new TreeNode();
node.Text = "福田分局";
node.Tag = "-1";
this.treeView1.Nodes.Add(node);//把指定节点添加到控件中
bindnode(node);
}
/// <summary>
/// 递归遍历指定节点下的子节点
/// </summary>
/// <param name="nd">参数,接收节点对象</param>
private void bindnode(TreeNode nd)
{
foreach (UmDept entity in entitys)
{
if (entity.parentId == int.Parse(nd.Tag.ToString()))
{
TreeNode node = new TreeNode();
node.Text = entity.deptName;
node.Tag = entity.deptId.ToString();
nd.Nodes.Add(node);//把指定节点添加到控件中
bindnode(node);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.bindtree("0"); //无限级分类
}
/// <summary>
/// 查找根节点(parent_ID为0的节点)的子节点
/// </summary>
/// <param name="parent_ID">参数,接收根节点ID</param>
private void bindtree(string parent_ID)
{
entitys = wsProxy.getAllDept();//获取所有树形数据
TreeNode node = new TreeNode();
node.Text = "福田分局";
node.Tag = "-1";
this.treeView1.Nodes.Add(node);//把指定节点添加到控件中
bindnode(node);
}
/// <summary>
/// 递归遍历指定节点下的子节点
/// </summary>
/// <param name="nd">参数,接收节点对象</param>
private void bindnode(TreeNode nd)
{
foreach (UmDept entity in entitys)
{
if (entity.parentId == int.Parse(nd.Tag.ToString()))
{
TreeNode node = new TreeNode();
node.Text = entity.deptName;
node.Tag = entity.deptId.ToString();
nd.Nodes.Add(node);//把指定节点添加到控件中
bindnode(node);
}
}
}