已回答n次了表结构:
id, parentid, name, navigetUrl, target,...
先读到数据集中,然后调用下面的函数:
FillTree(DataSet1.xxTable, TreeView1.Nodes, "根的父结点ID,比如-1", "父结点字段名", "排序字段名", "显示文本字段名", "传到新页面的值字段名", NavigetUrl, Target);
适用于框架结构,如果不是这种结构,自己改一下就是了。/// <summary>
/// 填充树控件
/// </summary>
/// <param name="dt">树控件结构所在表</param>
/// <param name="tns">节点集合</param>
/// <param name="strParentID">父节点值</param>
/// <param name="strParentField">父节点字段</param>
/// <param name="strSortField">排序字段</param>
/// <param name="strTextField">显示文本字段</param>
/// <param name="strValueField">值字段</param>
/// <param name="strNavigate">导航页</param>
/// <param name="strTarget">导航目标</param>
public void FillTree(DataTable dt, TreeNodeCollection tns, string strParentID, string strParentField, string strSortField, string strTextField, string strValueField, string strNavigate, string strTarget)
{
    TreeNode tn = null;
    DataRow[] drs = dt.Select(string.Format("{0}={1}", strParentField, strParentID), strSortField);    foreach (DataRow dr in drs)
    {
       tn = new TreeNode();
       tn.Text = dr[strTextField].ToString();
       tn.Value = dr[strValueField].ToString();
       tn.NavigateUrl = string.Format("{0}?P={1}", strNavigate, dr[strValueField]);
       tn.Target = strTarget;
       tns.Add(tn);       FillTree(dt, tn.ChildNodes, dr[strValueField].ToString(), strParentField, strSortField, strTextField, strValueField, strNavigate, strTarget);
   }
}

解决方案 »

  1.   

    public void TheardStart()
    {
    if(this.treeView1.SelectedNode.Text!="我的电脑")
    {
    string Path=this.treeView1.SelectedNode.FullPath;
    Path=Path.Substring(@"我的电脑\".Length);
    TreeNodeCollection tnc=this.treeView1.SelectedNode.Nodes;
    string[] s=Directory.GetDirectories(Path);//GetFileSystemEntries
    AddControl(Path);
    if(this.treeView1.SelectedNode.Nodes.Count<1)
    {
    for(int i=0;i<s.Length;i++)
    {
    try
    {
    SetNode(tnc,s[i].Substring(Path.Length).Replace("\\",""));
    }
    catch(System.Exception ee)
    {
    SetNode(tnc,s[i].Substring(Path.Length));
    }
    }
    }
    }
      

  2.   

    DataSet ds=pc.F_Jcsj_ExecSqlstring("select  lbbm, lbmc from xtwh_sjzd_lb" );
    for(int i=0;i<ds.Tables[0].Rows.Count;i++)

    TreeNode trn=new TreeNode();
    trn.Text=ds.Tables[0].Rows[i]["lbmc"].ToString();
    TrV_GJBZ.Nodes.Add(trn); DataSet ds1=pc.F_Jcsj_ExecSqlstring("select nbbm, mx ,bm from xtwh_sjzd_bm where lbbm='"+ds.Tables[0].Rows[i]["lbbm"].ToString().Trim()+"' order by xh");
    for(int j=0;j<ds1.Tables[0].Rows.Count;j++)
    {
    TreeNode tr=new TreeNode();
    tr.Text=ds1.Tables[0].Rows[j][1].ToString();
    tr.NavigateUrl="W_jwxt_jcsj_DataShow.aspx?NBBM="+ds1.Tables[0].Rows[j]["nbbm"].ToString()+"&bm="+ds1.Tables[0].Rows[j]["bm"].ToString();
    tr.Target="myFrame";
    TrV_GJBZ.Nodes[i].Nodes.Add(tr);
    }
    这是用FOR来动态生成的
      

  3.   

    DataFactory temp;
                try
                {
                    temp = new DataFactory("AlarmSystem");
                    this.areaTable = temp.FetchTable("TArea");
                }
                catch (DbException)
                {                
                    //初始化失败,请检查数据源是否正常工作
                    throw;
                }            //根节点,只能有一个根节点,即只有一笔记录的ParentAreaID为0
                TreeNode rootNode = new TreeNode();            if (this.areaTable.Rows.Count == 0) // 表为空,就创建一个根节点
                {
                    //创建根节点,设置其ParentAreaID为0,更新到数据库中
                    rootNode = new TreeNode("Default root");
                    rootNode.Name = "-1";
                    rootNode.Tag = new AreaExtensionInfo(0, "");                DataTable newTable = this.areaTable.Clone();
                    DataRow newRow = newTable.NewRow();
                    newTable.Columns.Remove("AreaID");                
                    newRow["AreaName"] = rootNode.Text;
                    newRow["ParentAreaID"] = 0;
                    newTable.Rows.Add(newRow);
                    newTable.PrimaryKey = new DataColumn[] { };
                    //可能有bug
                                    
                    try
                    {
                        temp = new DataFactory("AlarmSystem");
                        ArrayList list = temp.UpdateData("ProcInsertTArea", this.areaColumnName, newTable, true);                    this.rootNodeAreaId = Convert.ToInt32(list[0]);
                        rootNode.Name = list[0].ToString();
                    }
                    catch (DbException)
                    {
                        throw;
                    }
                }
                else
                {
                    // 根据存储在数据库中用户设置的根节点信息创建根节点
                    DataView rootRow = new DataView(this.areaTable);
                    rootRow.RowFilter = "ParentAreaID = 0";
                    // 唯一的根节点存在
                    if (rootRow.Count == 1)
                    {
                        foreach (DataRowView row in rootRow)
                        {
                            rootNode.Name = row["AreaID"].ToString();
                            this.rootNodeAreaId = Convert.ToInt32(row["AreaID"]);
                            rootNode.Text = row["AreaName"].ToString();
                            rootNode.Tag = new AreaExtensionInfo(row["ParentAreaID"], row["Description"]);
                        }
                    }
                    else
                    {
                        // 数据库信息错误
                        throw new DBRecordException("DataTable TArea record error");
                    }
                }            treeViewArea.Nodes.Add(rootNode);
                BuildTree(rootNode);
                rootNode.Expand();
                // 把属于当前Node中的Tag加入到ListView中            
                ShowAlarmTagInListView(rootNode);