生成树的两种递归方式


        protected void Page_Load(object sender, EventArgs e)
        {
            string connecting = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            DataSet ds = new DataSet();
            try
            {
                using (SqlConnection conn = new SqlConnection(connecting))
                {
                    conn.Open();
                    string sql = "select * from dbo.Node";

                    using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
                    {
                        da.Fill(ds);
                    }
                    conn.Close();
                }
            }
            catch (Exception err)
            {

                throw new Exception(err.Message + err.StackTrace);
            }
            //绑定一级节点 通过dataviw来实现
            TreeNode firstNode = new TreeNode();
            firstNode.Value = "-1";
            firstNode.Text = "项目简介 ";
            tvTreeView.Nodes.Add(firstNode);
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = "NodeParentId='-1'";
            foreach (DataRowView dr in dv)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = dr["NodeName"].ToString();
                rootNode.Value = dr["NodeId"].ToString();
                tvTreeView.Nodes.Add(rootNode);
                CreateChildNodes(rootNode.Value, ds.Tables[0], rootNode);
            }

        }
        protected void CreateChildNodes(string parentId,DataTable dt,TreeNode parentNode)
        {
            DataView dv= dt.DefaultView;
            dv.RowFilter = "NodeParentId='" +parentId+"'";
            foreach (DataRowView dr in dv)
            {
                TreeNode childNode = new TreeNode();
                childNode.Text = dr["NodeName"].ToString();
                childNode.Value = dr["NodeId"].ToString();
                parentNode.ChildNodes.Add(childNode);
                CreateChildNodes(childNode.Value, dt, childNode);
            }
           
        }
    }

 

 

第二种:

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string constring = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
                string sql = "select * from Node";
                DataSet ds = new DataSet();
                using (SqlConnection conn = new SqlConnection(constring))
                {
                    conn.Open();
                    using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
                    {
                        da.Fill(ds);
                    }
                    conn.Close();
                }
                DataTable dt = ds.Tables[0];
                TreeNode node = new TreeNode();
                node.Text = "项目简介 ";
                node.Value = "-1";
                tvBind.Nodes.Add(node);
                //绑定一级节点
                //第一种方法用dataRow
                DataRow[] rowlist = dt.Select("NodeParentId='-1'");
                foreach (DataRow dr in rowlist)
                {
                    TreeNode rootNode = new TreeNode();
                    rootNode.Value = dr["NodeID"].ToString();
                    rootNode.Text = dr["NodeName"].ToString();
                    rootNode.Expanded = false;
                    tvBind.Nodes.Add(rootNode);
                    CreateChildNode(rootNode, Convert.ToInt32(rootNode.Value), dt);
                }
            }
          

        }
        /// <summary>
        /// 递归找出节点id
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeId"></param>
        protected void CreateChildNode(TreeNode node,int parentId,DataTable dt)
        {
            DataRow[] rowList = dt.Select("NodeParentId='" + parentId + "'");
            foreach (DataRow dr in rowList)
            {
                TreeNode childNode = new TreeNode();
                childNode.Value = dr["NodeID"].ToString();
                childNode.Text=dr["NodeName"].ToString();
                node.ChildNodes.Add(childNode);
                CreateChildNode(childNode, Convert.ToInt32(childNode.Value), dt);
            }
        }

 

posted @ 2012-06-17 10:55  王永华  阅读(668)  评论(0编辑  收藏  举报