一个6层的树的递归终于写完了!(可扩展到n层)大家提点建议!

悟空师傅来了 2002-12-28 03:13:55
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
find sourcepage = (find)Context.Handler;
strwhere = sourcepage.returnstrsql;
}

string str_PatientName,str_CaseKind,str_CaseId,str_CaseNo,str_PageKind,str_PageNo;
string strsql;
strsql = "select * from Case_Main_Info where " + strwhere + " order by Patient_Name,Case_Kind,Case_Id,Case_No";
code code = new code();

str_PatientName = "";
str_CaseKind = "";
str_CaseId = "";
str_CaseNo = "";
str_PageKind = "";
str_PageNo = "";
int i_PatientName,i_CaseKind;
foreach(DataRowView drv_PatientName in code.findTable(strsql,"Case_Main_Info"))
{
i_PatientName = 0;
i_CaseKind = 0;
TreeNode node = new TreeNode();
//if(str_PatientName != drv_PatientName["Patient_Name"].ToString().Trim())
{
str_PatientName = drv_PatientName["Patient_Name"].ToString().Trim();
CreateNode(str_PatientName,str_PatientName,null);
i_PatientName = 1;//添加了PatientName,必须添加CaseKind
}
//if(str_CaseKind != drv_PatientName["Case_Kind"].ToString().Trim() || i_PatientName == 1)
{
str_CaseKind = drv_PatientName["Case_Kind"].ToString().Trim();
CreateNode(str_CaseKind,str_PatientName + "-" + str_CaseKind,str_PatientName);
i_CaseKind = 1;//添加了CaseKind,必须添加Id
}
//if(str_CaseId != drv_PatientName["Case_Id"].ToString().Trim() || i_CaseKind ==1)
{
str_CaseId = drv_PatientName["Case_Id"].ToString().Trim();
CreateNode(str_CaseId,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId,str_PatientName + "-" + str_CaseKind);
}
str_CaseNo = "第【" + drv_PatientName["Case_No"].ToString().Trim() + "】次";
CreateNode(str_CaseNo,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId + "-" + str_CaseNo,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId);

string strpagekind;
code code1 = new code();

strpagekind = "select * from Page_Info where Id = '" + drv_PatientName["Id"].ToString().Trim() + "' order by Page_Index";
foreach(DataRowView drv_PageKind in code1.findTable1(strpagekind,"Page_Info"))
{
if(str_PageKind != drv_PageKind["Page_Kind"].ToString().Trim())
{
str_PageKind = drv_PageKind["Page_Kind"].ToString().Trim();
CreateNode(str_PageKind,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId + "-" + str_CaseNo + "-" + str_PageKind,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId + "-" + str_CaseNo);
}
str_PageNo = "第【" + drv_PageKind["Page_No"].ToString().Trim() + "】页";
CreateNode(str_PageNo,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId + "-" + str_CaseNo + "-" + str_PageKind + "-" + str_PageNo,str_PatientName + "-" + str_CaseKind + "-" + str_CaseId + "-" + str_CaseNo + "-" + str_PageKind);
}
}
}
//
//创建节点
//
void CreateNode(string text,string id,string Pid)
{
TreeNode node=new TreeNode();
node.Text=text;
node.ID=id;

if(Pid == null)
{
TreeView1.Nodes.Add(node);
}
else
{
TreeNode Pnode=GetNode(Pid,TreeView1.Nodes);
Pnode.Nodes.Add(node);
}
}
//
//得到节点位置
//
TreeNode GetNode(string id,TreeNodeCollection tnc)
{
foreach(TreeNode node in tnc)
{
if(node.ID==id)
return node;
if(node.Nodes.Count!=0)
{
TreeNode tn=GetNode(id,node.Nodes);
if(tn!=null)
return tn;
}
}
return null;
}

其中findTable()是我的查询库的函数。
多谢江雨.net的帮忙,其中有心血!
再次谢谢江雨了!!领分吧!
...全文
101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
muddledman 2003-01-23
  • 打赏
  • 举报
回复
先收藏!
ar7_top 2002-12-30
  • 打赏
  • 举报
回复
看着头晕,巨讨厌没有注释的代码



先收藏,等以后头脑清醒的时候慢慢研究

:)
eshao 2002-12-28
  • 打赏
  • 举报
回复
递归应该是无穷层的。
tingningpower 2002-12-28
  • 打赏
  • 举报
回复
暂时看不懂!
hgknight 2002-12-28
  • 打赏
  • 举报
回复
哈哈,i am coming
Lostinet 2002-12-28
  • 打赏
  • 举报
回复
id,pid这样的结构,用一个Hastable递归一下就是无限树了。。
悟空师傅来了 2002-12-28
  • 打赏
  • 举报
回复
if前面的"//"要去掉,那是用来判断的!

62,041

社区成员

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

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

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

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