C# 最原始的tree 遞迴使用

來源:互聯網
上載者:User

標籤:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EasyUITree
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        List<tree> listTreeAll = new List<tree>();

        tree treeAll = new tree();

        public tree BindNew(tree node)
        {
            DataTable dr = GetReader(node.id);
            tree n = new tree();
            for (int i = 0; i < dr.Rows.Count; i++)
            {
                if (Convert.ToInt32(dr.Rows[i]["pid"]) == 0)
                {

                    n.id = Convert.ToInt32(dr.Rows[i]["id"]);
                    n.text = dr.Rows[i]["text"].ToString();
                    n.pid = Convert.ToInt32(dr.Rows[i]["pid"]);
                    n.child = GetChild(n);

                }
            }
            return n;
        }
        public List<tree> GetChild(tree node)
        {
            DataTable dr = GetReader(node.id);
            List<tree> child = new List<tree>();
            for (int i = 0; i < dr.Rows.Count; i++)
            {

                tree n = new tree();
                n.id = Convert.ToInt32(dr.Rows[i]["id"]);
                n.text = dr.Rows[i]["text"].ToString();
                n.pid = Convert.ToInt32(dr.Rows[i]["pid"]);
                child.Add(n);
                DataTable dr1 = GetReader(n.id);
                if (dr1 != null)
                {
                    n.child = GetChild(n);
                }


            }
            return child;
        }

        





        /// <summary>
        /// 測試
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            tree model = new tree();
            model.id = 0;
            BindNew(model);//遞迴

            List<tree> list = new List<tree>();
            list.Add(BindNew(model));




        }

        public DataTable GetReader(int pid)
        {
            string sql = " select * from t_tree where pid = " + pid + "  ";  //where pid = " + pid + "  ";
            string ConnectionString = "uid=sa;pwd=qazwsx;initial catalog=TestDBase;data source=DESKTOP-HKIRA54;Connect Timeout=900";
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);

                DataTable table = ds.Tables[0];
                return table;
            }
        }
    }


    public class tree
    {
        public int id { get; set; }
        public string text { get; set; }
        public int pid { get; set; }
        public List<tree> child { get; set; }


    }

C# 最原始的tree 遞迴使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.