有个字段关联就好办了.首先判定父节点.
然后根据父节点的与子节点的关系.进行用父节点..Add.子节点字段..

解决方案 »

  1.   

    参考
    动态创建菜单的代码: protected void Page_Load(object sender, EventArgs e)
        {
            strcon.Open();
            //特价商品
            string newque = "select top 2 * from tb_goods where newgoods=0 ";
            DataList1.DataSource = DataBase.ReDataSet(newque);
            DataList1.DataKeyField = "id";
            DataList1.DataBind();
            //新品上架
            string newquenew = "select top 4 * from tb_goods where newgoods=1 ";
            DataList2.DataSource = DataBase.ReDataSet(newquenew);
            DataList2.DataKeyField = "id";
            DataList2.DataBind();
            //添加树状视图
            string supertype="select * from tb_supertype";
            string subtype = "select * from tb_subtype";
            DataSet dssuper = DataBase.ReDataSet(supertype);
            DataSet dssub = DataBase.ReDataSet(subtype);
            DataRow[] rows = dssuper.Tables[0].Select();
            DataRow[] subrows = dssub.Tables[0].Select();
            if (!IsPostBack)
            {
                foreach(DataRow row in rows)
                {
                    TreeNode nd=new TreeNode();
                    nd.Text=row["typename"].ToString();
                    nd.Value = row["id"].ToString();
                    TreeView1.Nodes.Add(nd);
                    foreach (DataRow subrow in subrows)
                    {
                        TreeNode subnd = new TreeNode();
                        subnd.Text = subrow["typename"].ToString();
                        subnd.Value = subrow["id"].ToString();
                        if (subrow["supertype"].ToString() == row["id"].ToString())
                        {
                            nd.ChildNodes.Add(subnd);
                        }
     
                    }
                }
            }
            strcon.Close();
        }
      

  2.   

    如果只要3级的话是很简单的
    数据库字段
    id name parentid deepth
    代码:
                    DetailTreat_pro pro = new DetailTreat_pro();
                    //这里得到数据库的数据返回一个DataSet
                    DataSet set = pro.CreateTree();
                    foreach (DataRow row in set.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row[3]) == 0)
                        {
                            TreeNode node = new TreeNode();
                            node.Text = row[1].ToString();
                            this.TreeView1.Nodes.Add(node);
                        }
                    }
                    foreach (DataRow row in set.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row[3]) == 1)
                        {
                            TreeNode node = new TreeNode();
                            node.Text = row[1].ToString();
                            //这里得到父节点的id,返回一个arrylist
                            arr = pro.GetId(Convert.ToInt32(row[0]));
                            foreach (ProType pt in arr)
                            {
                                if (pt.Deepth == 2)
                                {
                                    TreeNode node_1 = new TreeNode();
                                    node_1.Text = pt.Pname1.ToString();
                                    node.ChildNodes.Add(node_1);
                                }
                            }
                            arr.Clear();
                            this.TreeView1.Nodes[Convert.ToInt32(row[2]) - 1].ChildNodes.Add(node);
                        }
                    }
    写得可能麻烦了点,不过可以分出3级