give you a example ,if you have a dataset contains columns id , name , parent_id,you can do like this :
#region CreateTree
/// <summary>
/// Create a tree by given root node from a dataset , if the dataset doesn't contains the id , it will throw a ISDException
/// </summary>
/// <param name="oDsNodes">Source DataSet contains columns id , name , parent_id</param>
/// <param name="nID">The root of the tree</param>
/// <returns></returns>
public string CreateTree(DataSet oDsNodes , int nID)
{
string sReturn = "<Node ID='" + nID.ToString();
string sTemp = "";
bool bExsit = false;
for(int i = 0 ; i < oDsNodes.Tables[0].Rows.Count ; i++)
{
int nParentID = int.Parse(oDsNodes.Tables[0].Rows[i]["PARENT_ID"].ToString());
int nTempID = int.Parse(oDsNodes.Tables[0].Rows[i]["ID"].ToString());
if(nID == nParentID)
{
sTemp+= CreateTree(oDsNodes , nTempID);
}
if(nID == nTempID)
{
string sTempName = oDsNodes.Tables[0].Rows[i]["NAME"].ToString().Trim();
sReturn += "' Name='" + sTempName + "'>";
bExsit = true;
}
}
if(!bExsit)
{
throw new ISDException("Can not found this node");
}
sReturn += sTemp + "</Node>";
return sReturn;
}
#endregion
#region CreateTree
/// <summary>
/// Create a tree by given root node from a dataset , if the dataset doesn't contains the id , it will throw a ISDException
/// </summary>
/// <param name="oDsNodes">Source DataSet contains columns id , name , parent_id</param>
/// <param name="nID">The root of the tree</param>
/// <returns></returns>
public string CreateTree(DataSet oDsNodes , int nID)
{
string sReturn = "<Node ID='" + nID.ToString();
string sTemp = "";
bool bExsit = false;
for(int i = 0 ; i < oDsNodes.Tables[0].Rows.Count ; i++)
{
int nParentID = int.Parse(oDsNodes.Tables[0].Rows[i]["PARENT_ID"].ToString());
int nTempID = int.Parse(oDsNodes.Tables[0].Rows[i]["ID"].ToString());
if(nID == nParentID)
{
sTemp+= CreateTree(oDsNodes , nTempID);
}
if(nID == nTempID)
{
string sTempName = oDsNodes.Tables[0].Rows[i]["NAME"].ToString().Trim();
sReturn += "' Name='" + sTempName + "'>";
bExsit = true;
}
}
if(!bExsit)
{
throw new ISDException("Can not found this node");
}
sReturn += sTemp + "</Node>";
return sReturn;
}
#endregion
解决方案 »
- cms系统问题
- c#中, 返回代表十六进制数值的 String 的函数 是哪个
- C#中结构体的内存大小与分配问题~求解
- 上传专题N种方法 1 上传到oracle数据库
- 请教大家winform里面 读出.txt文本文件问题!!!!!!!!!!!
- 如何不触发click事件,用javascript提示textbox为空?
- 如何读取一个TXT文件,生成TREEVIEW目录
- 请教与access连接的连接字符串
- 如何判断用户输入的手机号码的有效性?
- 接口有什么用啊,语法格式很明白了,但是感觉没什么作用
- 为何我从Graphics保存的图片是全黑的?
- 请问我在webapplication中给一个IIS站点创建了一个子目录,并向里面拷贝一个首页,出现问题。
也可以从 TreeView inherit, 不用自己去写一个 TreeView吧
添加第一层节点
node=new TreeNode();
node.ID="";
node.Text="";
node.NodeData="0";
node.ImageUrl="";
node.NavigateUrl="";
TreeView1.Nodes.Add(node);
添加子节点
Node1=new TreeNode();
Node1.ID="";
Node1.Text="";
Node1.NodeData="0";
Node1.ImageUrl="";
Node1.NavigateUrl="";
node.Nodes.Add(Node1);
private void Page_Load(object sender, System.EventArgs e)
{}
过程中怎样实现上面的代码?