treeview 資料庫 遞迴

來源:互聯網
上載者:User

1:建立資料庫

我們在SQL SERVER 2000裡建立一個表tbTree,表的結構設計如下:
列名 資料類型 描述  長度 主鍵
ID Int 節點編號  4 是
ParentID Int 父節點編號  4 
ConText Nvarchar 我們要顯示的節點內容 50 

在SQL SERVER 2000中建表的指令碼:

CREATE TABLE [dbo].[tbTree] (
 [ID] [int] NOT NULL ,
 [Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [ParentID] [int] NULL
) ON [PRIMARY]

在表中添加如下記錄:

insert tbtree (ID,Context,ParentID)  values ( 1,'中國',0)
insert tbtree (ID,Context,ParentID)  values ( 2,'北京',11)
insert tbtree (ID,Context,ParentID)  values ( 3,'天津',11)
insert tbtree (ID,Context,ParentID)  values ( 4,'河北省',1)
insert tbtree (ID,Context,ParentID)  values ( 5,'廣東省',1)
insert tbtree (ID,Context,ParentID)  values ( 6,'廣州',5)
insert tbtree (ID,Context,ParentID)  values ( 7,'四川省',1)
insert tbtree (ID,Context,ParentID)  values ( 8,'成都',7)
insert tbtree (ID,Context,ParentID)  values ( 9,'深圳',5)
insert tbtree (ID,Context,ParentID)  values ( 10,'石家莊',4)
insert tbtree (ID,Context,ParentID)  values ( 11,'遼寧省',1)
insert tbtree (ID,Context,ParentID)  values ( 12,'大連',11)
insert tbtree (ID,Context,ParentID)  values ( 13,'上海',1)
insert tbtree (ID,Context,ParentID)  values ( 14,'天河軟體園',6)
insert tbtree (ID,Context,ParentID)  values ( 15,'汕頭',5)
**********************************************************************

2:資料繫結

public class Index : System.Web.UI.Page
 {    

          DataSet ds=new DataSet();
          protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
   
           private void Page_Load(object sender, System.EventArgs e)
          {
                // 在此處放置使用者代碼以初始化頁面
                SqlConnection CN = new SqlConnection();
                    try
                    {
                         //初始化連接字串
                         CN.ConnectionString="server=.;uid=sa;pwd=sa;database=Test";
                          CN.Open();
                         //添加命令,從資料庫中得到資料
                        SqlCommand sqlCmd= new SqlCommand();
                         sqlCmd.Connection = CN;
                         sqlCmd.CommandText = "select * from tbTree";
                         sqlCmd.CommandType = CommandType.Text ;
                         SqlDataAdapter adp = new SqlDataAdapter(sqlCmd);
                         adp.Fill(ds);
                     }
                      catch (Exception ex)
                     {
                         throw (ex);  
                       }
                       finally
                    {
                       CN.Close();
                     }
                           //調用遞迴函式,完成樹形結構的產生
                           //tn2=AddTree(0, (TreeNode)null);
                          //TreeView1.Nodes.Add(tn2);
                             AddTree(0, (TreeNode)null);

                                 }

public void AddTree(int ParentID,TreeNode pNode) 

{ 

      Microsoft.Web.UI.WebControls.TreeNode tn1=new TreeNode();
       DataView dvTree = new DataView(ds.Tables[0]);
        //過濾ParentID,得到當前的所有子節點
        dvTree.RowFilter =  "[PARENTID] = " + ParentID;

     foreach(DataRowView Row in dvTree)
      {
        if(pNode == null)
         {    //'?添加根節點
     
             tn1.Text=Row["ConText"].ToString();
             TreeView1.Nodes.Add(tn1);
               tn1.Expanded=true;
               AddTree(Int32.Parse(Row["ID"].ToString()),tn1);    //再次遞迴
           }
        else
          {          //添加當前節點的子節點
             Microsoft.Web.UI.WebControls.TreeNode tn2=new TreeNode();
             tn2.Text=Row["ConText"].ToString();
               pNode.Nodes.Add(tn2);
               tn1.Expanded=true;
              AddTree(Int32.Parse(Row["ID"].ToString()),tn2);    //再次遞迴
           }  

     }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.