首页 > 代码库 > WebForm中TreeView的使用

WebForm中TreeView的使用

protected void Page_Load(object sender, EventArgs e)
        {
            DatabaseBind();
            TreeView1.ShowLines = true;
            TreeView1.ExpandDepth = 1;
        }

        public void DatabaseBind()
        {
            string connName = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            SqlConnection conn = new SqlConnection(connName);
            string sql = "select * from Employees";
            SqlDataAdapter sda =new SqlDataAdapter (sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "Employees");

            TreeNode tree1 = new TreeNode("雇员信息");
            TreeView1.Nodes.Add(tree1);

            for (int i = 0; i < ds.Tables["Employees"].Rows.Count; i++)
            {
                TreeNode tree2 = new TreeNode(ds.Tables["Employees"].Rows[i][1].ToString(),
                                              ds.Tables["Employees"].Rows[1][1].ToString());
                tree1.ChildNodes.Add(tree2);
                for (int j = 0; j <ds.Tables["Employees"].Columns.Count ; j++)
                {
                    TreeNode tree3 = new TreeNode(ds.Tables["Employees"].Rows[i][j].ToString(),
                                              ds.Tables["Employees"].Rows[i][j].ToString());
                tree2.ChildNodes.Add(tree3);
                }

            }
        }

WebForm中TreeView的使用