博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TreeView控件的使用

Posted on 2006-04-03 22:02  Hello-Zhfen  阅读(494)  评论(0编辑  收藏  举报

使用TREEVIEW控件编写的一段代码,想了N久才出来

    protected void TreeView1_Load()
    {
        //用于填充TreeView1
        SqlCommand Mycommand1 = new SqlCommand("select *  from CHAPTER where Course_ID=0", Myconn);
        SqlDataReader DR1 = Mycommand1.ExecuteReader();
        //SqlDataAdapter DA1 = new SqlDataAdapter(Mycommand1);
        //DataSet DS1 = new DataSet("temp");
        //DA1.Fill(DS1);

        SqlCommand Mycommand2;
        SqlDataReader DR2;
       
       
        while(DR1.Read())
        {
            TreeNode NewNode = new TreeNode(DR1["Chapter_Name"].ToString());
            NewNode.Value = DR1["Chapter_ID"].ToString();
            NewNode.Collapse();
            //二层结点--->
            //Mycommand2 = new SqlCommand("select * from SECTION where Chapter_ID="+DR1["Chapter_ID"].ToString(),Myconn);
            //DR2 = Mycommand2.ExecuteReader();
            //TreeNode NewNode2 = new TreeNode(DR2["Section_Name"].ToString());
            //NewNode.ChildNodes.Add(NewNode2);
            //<<-----
           
            TreeView1.Nodes.Add(NewNode);

        }
        DR1.Close();

        foreach (TreeNode NewNode in TreeView1.Nodes)
        {
            Mycommand2 = new SqlCommand("select * from SECTION where Chapter_ID=" + NewNode.Value.ToString(), Myconn);
            DR2 = Mycommand2.ExecuteReader();
            while (DR2.Read())
            {
                TreeNode TempNode = new TreeNode(DR2["Section_Name"].ToString());
                TempNode.NavigateUrl = "#";
                TempNode.Target = "_blank";
                NewNode.ChildNodes.Add(TempNode);

            }
            DR2.Close();
        }
       

        /*
        if (DS1.Tables["CHAPTER"].Rows.Count.CompareTo(0)>0)
        {
                foreach (DataRow Row in DS1.Tables["CHAPTER"].Rows)
                {
                   TreeNode NewNode = new TreeNode(Row["Chapter_Name"].ToString());
                    //二层结点--->
                   Mycommand2 = new SqlCommand("select * from SECTION where Chapter_ID=" + Row["Chapter_Name"].ToString(), Myconn);
                   DR2 = Mycommand2.ExecuteReader();
                   TreeNode NewNode2 = new TreeNode(DR2["Section_Name"].ToString());
                   NewNode.ChildNodes.Add(NewNode2);
                    //<<---------
                   TreeView1.Nodes.Add(NewNode);
                }
        }
                DS1.Dispose();
       */
    }