一个6层的树的递归终于写完了!(可扩展到n层)大家提点建议!
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的帮忙,其中有心血!
再次谢谢江雨了!!领分吧!