没有必要建立多张表格
自己写的代码,仅供参考[email protected] Microsoft.Web.UI.WebControls.TreeNodeCollection GenerateNodes(bool WithRootNode,bool WithNodeData,bool WithNodeHL,string Status,string URL,string Target)
{
//Create Database Connection
BSDBConn db=new BSDBConn();
SqlConnection sqlconn=db.GetConnection(); //Get all information of ContentTypes
SqlCommand sc=new SqlCommand();
sc.Connection=sqlconn;
if(Status=="All")
{
sc.CommandText="SELECT Guid, ParentGuid, Name FROM ContentType WHERE (Name <> 'root')";
}
else if(Status=="Enable")
{
sc.CommandText="SELECT Guid, ParentGuid, Name FROM ContentType WHERE (Name <> 'root') AND (Enable = 1)";
}
else if(Status=="Disable")
{
sc.CommandText="SELECT Guid, ParentGuid, Name FROM ContentType WHERE (Name <> 'root') AND (Enable = 0)";
}
sqlconn.Open();
SqlDataReader sdr=sc.ExecuteReader();
DataTable ContentTypedt=new DataTable();
ContentTypedt.Columns.Add("Guid",typeof(System.Guid));
ContentTypedt.Columns.Add("ParentGuid",typeof(System.Guid));
ContentTypedt.Columns.Add("Name",typeof(string));

while(sdr.Read())
{
System.Data.DataRow dr=ContentTypedt.NewRow();
dr["Guid"]=sdr.GetGuid(0);
dr["ParentGuid"]=sdr.GetGuid(1);
dr["Name"]=sdr.GetString(2);
ContentTypedt.Rows.Add(dr);
}
sqlconn.Close();
sdr.Close(); System.Guid CurrentParentGuid=new Guid("C3BED2C9-A5B5-43f7-ABEC-C37C4F00F89D");
System.Guid CurrentGuid=System.Guid.Empty;
Microsoft.Web.UI.WebControls.TreeNode RootNode=new Microsoft.Web.UI.WebControls.TreeNode();
RootNode.Text="Root";
Microsoft.Web.UI.WebControls.TreeNode CurrentParentNode=RootNode;
int CurrentIndex=0;
System.Data.DataView dv=new DataView(ContentTypedt);
string tempName="";
bool Finished=false;
Microsoft.Web.UI.WebControls.TreeNodeCollection RootCollection=new Microsoft.Web.UI.WebControls.TreeNodeCollection();

解决方案 »

  1.   

    if(Status=="All"||Status=="Enable")
    {
    //Add all nodes into nodecollection


    if(ContentTypedt.Rows.Count!=0)
    {

    while(!Finished)
    {
    //Check is Current Node has Child
    dv.RowFilter="ParentGuid = '"+CurrentParentGuid+"'";
    dv.Sort="Name"; //have child
    if(dv.Count!=0)
    {
    try
    {
    tempName=dv[CurrentIndex].Row["Name"].ToString();
    }
    catch
    {
    if(CurrentParentGuid!=new Guid("C3BED2C9-A5B5-43f7-ABEC-C37C4F00F89D"))
    {
    //back to up level
    CurrentParentNode=(Microsoft.Web.UI.WebControls.TreeNode)CurrentParentNode.Parent;
    CurrentGuid=CurrentParentGuid;
    dv.RowFilter="Guid = '"+CurrentParentGuid+"'";
    dv.Sort="Name";
    CurrentParentGuid=new Guid(dv[0].Row["ParentGuid"].ToString());
    dv.RowFilter="ParentGuid = '"+CurrentParentGuid+"'";
    dv.Sort="Name";
    for(int i=0;i<dv.Count;i++)
    {
    if(CurrentGuid.ToString()==dv[i].Row["Guid"].ToString())
    {
    CurrentIndex=i+1;
    break;
    }
    }
    continue;
    }
    else
    {
    break;
    }
    }
    //have more than one child and current child is not null 
    if(dv.Count!=0&&tempName!=""&&tempName!=null)
    {
    CurrentGuid=new Guid(dv[CurrentIndex].Row["Guid"].ToString());
    //Add New Node to CurrentNodeCollection
    Microsoft.Web.UI.WebControls.TreeNode tn=new Microsoft.Web.UI.WebControls.TreeNode();
    tn.Text=dv[CurrentIndex].Row["Name"].ToString();
    if(WithNodeData)
    {
    tn.NodeData=dv[CurrentIndex].Row["Guid"].ToString();
    }
    if(WithNodeHL)
    {
    tn.NavigateUrl=URL+dv[CurrentIndex].Row["Guid"].ToString();
    tn.Target=Target;
    }
    CurrentParentNode.Nodes.AddAt(CurrentIndex,tn); //Check is Current Node has Child dv.RowFilter="ParentGuid = '"+CurrentGuid+"'";
    dv.Sort="Name";
    //have child(s)
    if(dv.Count!=0)
    {
    //to next level
    CurrentParentGuid=CurrentGuid;
    CurrentParentNode=CurrentParentNode.Nodes[CurrentIndex];
    CurrentIndex=0;
    continue;
    }
    //no child
    else
    {
    CurrentIndex++; 
    continue;
    }
    }
    //reach end of current node
      

  2.   

    else
    {
    if(CurrentParentGuid!=new Guid("C3BED2C9-A5B5-43f7-ABEC-C37C4F00F89D"))
    {
    //back to up level
    CurrentParentNode=(Microsoft.Web.UI.WebControls.TreeNode)CurrentParentNode.Parent;
    CurrentGuid=CurrentParentGuid;
    dv.RowFilter="Guid = '"+CurrentParentGuid+"'";
    dv.Sort="Name";
    CurrentParentGuid=new Guid(dv[0].Row["ParentGuid"].ToString());
    dv.RowFilter="ParentGuid = '"+CurrentParentGuid+"'";
    dv.Sort="Name";
    for(int i=0;i<dv.Count;i++)
    {
    if(CurrentGuid.ToString()==dv[i].Row["Guid"].ToString())
    {
    CurrentIndex=i+1;
    break;
    }
    }
    continue;
    }
    else
    {
    break;
    }
    }
    }
    }

    if(!WithRootNode)
    {
    Microsoft.Web.UI.WebControls.TreeNodeCollection NoRootCollection=new Microsoft.Web.UI.WebControls.TreeNodeCollection();
    foreach(Microsoft.Web.UI.WebControls.TreeNode tn in RootNode.Nodes)
    {

    NoRootCollection.Add((Microsoft.Web.UI.WebControls.TreeNode)tn.Clone());
    }
    RootCollection=NoRootCollection; }
    else
    {
    RootCollection.Add(RootNode);
    }
    }
    } //user want TreeNode which is disabled
    else if(Status=="Disable")
    {
    foreach(DataRow dr in ContentTypedt.Rows)
    {
    Microsoft.Web.UI.WebControls.TreeNode tn=new Microsoft.Web.UI.WebControls.TreeNode();
    tn.Text=dr["Name"].ToString();
    if(WithNodeData)
    {
    tn.NodeData=dr["Guid"].ToString();
    }
    if(WithNodeHL)
    {
    tn.NavigateUrl=URL+dr["Guid"].ToString();
    tn.Target=Target;
    }
    RootCollection.Add(tn);
    }
    }
    return RootCollection;
    }