各位大哥,我现在想把数据表的数据用树(TreeView)显示出来.
数据表结构如下.
id fatherid path name
1   0        1    部门A
2   1        1,2  部门B
3   0        3    部门C
4   3        3,4  部门D
5   4        3,4,5 部门E树的结构应该是
部门A
 -部门B
部门C
 -部门D
  -部门E
///////////////////////
请问我怎么把这张表的数据用TreeView显示出来啊!!急!!

解决方案 »

  1.   

    两种方法,一种是用迭代,实现GetChildrenByParentID(ParentID)方法,然后通过递归绑定到树上。
    第二种方法是通过继承IHierarchicalDataSource接口实现一个TreeView的数据源控件,这个数据源控件可以直接拖拽到页面上,通过设置TreeView的DataSourceID就可以了,第二种方式的参考代码如下:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using EquipMIS.BusinessLogic;
    using EquipMIS.BusinessLogic.Entities;
    using System.Security.Permissions;
    using System.Security;namespace EquipMIS.WebControls
    {
        public class CLocationDataSource : HierarchicalDataSourceControl, IHierarchicalDataSource
        {
            public CLocationDataSource() : base() { }        // Return a strongly typed view for the current data source control.
            private CLocationDataSourceView view = null;        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
            {
                // 这里的viewPath保存的是父节点的键值,但是根节点的viewPaht为空,所以要进行处理
                viewPath = string.IsNullOrEmpty(viewPath) ? m_StartPosition.ToString() : viewPath;
                view = new CLocationDataSourceView(viewPath);
                return view;
            }
            // This can be used declaratively. To enable declarative use, 
            // override the default implementation of CreateControlCollection 
            // to return a ControlCollection that you can add to.
            protected override ControlCollection CreateControlCollection()
            {
                return new ControlCollection(this);
            }
        }
        public class CLocationDataSourceView : HierarchicalDataSourceView
        {        private string _viewPath;
            public CLocationDataSourceView(string viewPath)
            {
                _viewPath = string.IsNullOrEmpty(viewPath) ? "0" : viewPath;        }        // 这个方法是核心方法,迭代体
            public override IHierarchicalEnumerable Select()
            {
                CLocationCollection collection = new CLocationCollection();
                foreach (CLocation category in CLocation.GetCatalogs())
                {
                    if (category.ParentID.ToString() == _viewPath)
                        collection.Add(category);
                }            return collection;
            }
        }}
      

  2.   

    DataTable = 所有数据
    Datatable.select(fatherid=0)
    foreach( datarow r in datatable.rows )
    {
          TreeNode node = new Treenode
          node.text = r["name"];
          node.value = r["id"];
          treeview1.nodes.add(node);
          datarow[] rows = datatable.select("fatherid="+r["id"])
         if( rows.length > 0 )
         {
             InitTree( rows,node);
         }
    }void InitTree( datarow[] rows , treenode node )
    {
        foreach( datarow r in rows )
        {
             TreeNode node = new treenode
             node.text  = r["name"]
             node.value = r["id"]
             treeview1.nodes.add(node);
             datarow[] rs = datatable.select("fatherid="+r["id"])
             if( rows.length > 0 )
                  InitTree( rs , node )
         }
    }
      

  3.   

     更正一下 InitTree函数,应该是
    void InitTree( datarow[] rows ,treenode node )
    中的
    treeview1.nodes.add应该改为
    node.ChildNodes.add