没有必要建立多张表格
自己写的代码,仅供参考[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();
自己写的代码,仅供参考[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();
解决方案 »
- 给定richtextbox的行号 怎么获取该行的字符串!急!!
- 运行出错没有EXEC的定义(部分源码)
- 经纬度转化为大地坐标 求代码
- ★300分(追加帖子给分)!!求简单的查询分析器例程!★
- ClickOnce发布应用程序对服务器有什么要求
- 请教:如何把一个字节数组转成字符串,再从字符串转成字节数组?
- 真的是一个很简单的问题啊
- C#里面怎么在两个类里面传递变量值
- 如何利用c#打印BMP\JPG图象
- 哪位大虾有办法在修改DataGrid,让它也支持Combox?高分相赠!
- SharpDevelopFidalgoRC1Setup.exe 的程序中怎么没有界面设计了(design)?
- 在DataGrid中放了两个LinkButton,请教怎么触发他们的click事件
{
//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
{
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;
}