C# 最简单的递归

public void AddTree(int ParentID, TreeNode pNode) 
{
TreeNode tn1 = new TreeNode();
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach (DataRowView Row in dvTree)
{
if (pNode == null)
{ //''?添加根节点

tn1.Text = Row["name"].ToString();
TreeView1.Nodes.Add(tn1);
tn1.Expanded = true;
AddTree(Int32.Parse(Row["ID"].ToString()), tn1); //再次递归
}
else
{ //添加当前节点的子节点
TreeNode tn2 = new TreeNode();
tn2.Text = Row["name"].ToString();
//pNode.Nodes.Add(tn2);
pNode.ChildNodes.Add(tn2);
tn1.Expanded = true;
AddTree(Int32.Parse(Row["ID"].ToString()), tn2); //再次递归
}

}

}
原文地址:https://www.cnblogs.com/dullbaby/p/5038547.html