在线急等,关于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); ////这句有问题...
}
}
...全文
92 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
CreatChildTree(tn); ////这句有问题...

CreatChildTree(tn2);
tangjinzn 2008-09-03
  • 打赏
  • 举报
回复
table、tab:查询返回的DataTable;

//生成父级节点
TreeNode fatherNode = new TreeNode();
fatherNode.Text = table.Rows[0]["CatName"].ToString() ;
fatherNode.Value = table.Rows[0]["CatID"].ToString();
fatherNode.Expanded = true;
TreeView2.Nodes.Add(fatherNode);
//生成第一级子节点
foreach (DataRow dr in tab.Rows)
{
TreeNode Child = new TreeNode();
Child.Text = dr["CatName"].ToString();
Child.Value = dr["ParentID "].ToString();
fatherNode.ChildNodes.Add(Child);
//生成第二级子节点
foreach (DataRow drs in table.Rows)
{
TreeNode childNode = new TreeNode();
childNode.Text = drs["CatName"].ToString();
childNode.Value = drs["ParentID"].ToString();
Child.ChildNodes.Add(childNode);
}
}
参考。。
gdjlc 2008-09-03
  • 打赏
  • 举报
回复
提示:超时时间已到。超时时间已到,但是尚未从池中获取连接。
另外我把SqlDataReader改为DataSet等时就提示不能无限递归...

是不是要在代码中判断下级目录是否存在?是的话怎么加?
freeflying1222 2008-09-03
  • 打赏
  • 举报
回复
报的什么错,贴出来噻!
gdjlc 2008-09-03
  • 打赏
  • 举报
回复
是不限树的级数的,有多少级就取多少的.
浮生若梦丶 2008-09-03
  • 打赏
  • 举报
回复
请各位大侠帮我修改下代码,CreatChildTree(TreeNode tn)中加入CreatChildTree(tn); 就运行错误,不加它就只能显示两级的目录,是不是要加什么判断条件啊??
不知道要不要设树的级数能有几级,看看和这个有关不
gdjlc 2008-09-03
  • 打赏
  • 举报
回复
谢谢大家的热心帮助,由于分数不多,只能将就点了.

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧