asp.net treeview 动态读取数据库

        TreeNode _node = new TreeNode();
        TreeNode noderoot = new TreeNode();
        if (TreeView1.Nodes.Count == 0)                  //添加根节点
        {
            noderoot.Text = "所有系统结构";              
            noderoot.Value = "0";
            TreeView1.Nodes.Add(noderoot);
            noderoot.ExpandAll();                          //展开所有节点
        }
        sql = "select [字段1],[字段2] from [table]";   //从数据库中读出子节点
        DataTable dt = db.GetDataTable(sql);
        DataView dv = new DataView(dt);
        foreach (DataRowView drv in dv)
        {
            _node = new TreeNode();
            _node.Text = drv[字段2]ToString();
            _node.Value = drv[字段1].ToString();
            noderoot.ChildNodes.Add(_node);

      }

posted on 2008-12-08 16:55  sherry  阅读(513)  评论(0编辑  收藏  举报