你想做到什么效果
树的节点都要从数据库读出来吗?
那你可以用递归调用
public void getSysfunTree(int id, TreeNode sp)
{
SqlConnection con = new SqlConnection(str);
string sql = "select * from sysfun where parentnodeid=" + id;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "sysfun");
foreach (DataRow r in ds.Tables[0].Rows)
{
TreeNode node = new TreeNode();
node.Text = r["DisplayName"].ToString();
node.NavigateUrl = r["NodeURL"].ToString();
if (sp == null)
{
this.TreeView1.Nodes.Add(node);
}
else
{
sp.ChildNodes.Add(node);
}
getSysfunTree(Convert.ToInt32(r["NodeId"]), node);
}