在线急等,关于TreeView遍历树的问题...
gdjlc 2008-09-03 11:03:52 Categories表中字段为CatID,CatName,ParentID
------------------------------------------------------
//获取行业的父行业
public static SqlDataReader GetParCategory()
{
string strSql = @"select * from Categories where ParentID=0";
return SqlHelper.ExecuteReader(strSql);
}
//获取行业的子行业
public static SqlDataReader GetChildCategory(int catid)
{
string strSql = @"select * from Categories where ParentID=" + catid;
return SqlHelper.ExecuteReader(strSql);
}
-------------------------------------------------------------
请各位大侠帮我修改下代码,CreatChildTree(TreeNode tn)中加入CreatChildTree(tn); 就运行错误,不加它就只能显示两级的目录,是不是要加什么判断条件啊??
private void CreatTree()
{
if (!IsPostBack)
{
SqlDataReader dr1 = ADRelease.BLL.Enterprise.GetParCategory();
while(dr1.Read())
{
TreeNode tn = new TreeNode();
tn.Text = dr1["CatName"].ToString();
tn.Value = dr1["CatID"].ToString();
TreeView1.Nodes.Add(tn);
CreatChildTree(tn);
}
}
}
private void CreatChildTree(TreeNode tn)
{
int catid = int.Parse(tn.Value);
Label1.Text = tn.Value;
SqlDataReader dr2 = ADRelease.BLL.Enterprise.GetChildCategory(catid);
while(dr2.Read())
{
TreeNode tn2 = new TreeNode();
tn2.Text = dr2["CatName"].ToString();
tn2.Value = dr2["CatID"].ToString();
tn.ChildNodes.Add(tn2);
CreatChildTree(tn); ////这句有问题...
}
}