标题:三层架构中UI如何实现treeview绑定,怎么递归...
只看楼主
hans998123
Rank: 2
等 级:论坛游民
帖 子:20
专家分:14
注 册:2011-7-10
结帖率:0
 问题点数:0 回复次数:2 
三层架构中UI如何实现treeview绑定,怎么递归...
DAL
public List<TreeModel> GetAllTree()
        {
            string sql = "select * from T_department";
            List<TreeModel> list = new List<TreeModel>();
            DataTable dt = SqlHelper.ExecuteDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                TreeModel tm = RowToModel(dr);
                list.Add(tm);
            }
            return list;
        }
        private TreeModel RowToModel(DataRow dr)
        {
            TreeModel tm = new TreeModel();
            tm.D_Cid = dr["d_cid"].ToString();
            tm.D_Id = dr["d_id"].ToString();
            tm.D_Name = dr["d_name"].ToString();
            tm.D_Pid = dr["d_pid"].ToString();
            return tm;
        }
BLL
public class TreeBLL
    {
        TreeDAL dal = new TreeDAL();
        public List<TreeModel> GetAllTree()
        {
            return dal.GetAllTree();
        }
    }
UI中返回List<TreeModel>,请大神帮忙写下UI  谢谢
表是这么设计的
A    总经办            1    0
AA    生产部            11    1
AAA    保修部            111    11
BAA    非保部            211    11
BA    物流部            21    1
ABA    物流一部    121    21
BBA    物流二部    221    21
CBA    物流三部    321    21
CA    市场部            31    1
ACA    市场拓展    131    31
BCA    营销部            231    31
ABCA    电器营销部    1231    231
BBCA    电子营销部    2231    231
搜索更多相关主题的帖子: sql private return 如何 
2012-11-17 13:42
smart0721
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:106
专家分:468
注 册:2012-2-10
得分:0 
首先你没把部门节点之间的父子关系说清楚,别人刚看到会一头雾水!我想你表后面那两个字段应该表示其部门id和其父部门id
你DAL跟BLL这样设计有点问题
我想应该这样设计会比较好
DAL
  public DataTable GetData()
  {
    string sql = "select * from T_department";
    DataTable dt = SqlHelper.ExecuteDataTable(sql);
//这你要先定义好这个ExecuteDataTable这个方法,连接数据库并查找表并返回dataTable的方法
    return dt;
   }
Model
public class TreeModel
    {
        public string D_Cid  { get; set; }              
        public int D_Id  { get; set; }     
        public string D_Name { get; set; }         
        public int D_Pid { get; set; }     //
        public List<TreeModel> TreeModels { get; set; }
    }
BLL
using *.DAL;//先添加该DAL层项目

   public class TreeBLL
    {
      public List<TreeModel> GetAllTree()
      {
        TreeDAL dal = new TreeDAL();
        List<TreeModel> list = new List<TreeModel>();
        DataTable dt=dal.GetData();
        for(int i = 0; i < dt.Rows.Count; i++)
        {
//暂且认为表T_department最后两列的字段名分别为d_id和d_pid
          if (Convert.ToInt32(dt.Rows[i]["PerentID"]) == 0)
        {
          TreeModel tm = new TreeModel();
          tm.D_Cid = dr["d_cid"].ToString();
          tm.D_Id = dr["d_id"].ToString();
          tm.D_Name = dr["d_name"].ToString();
          tm.D_Pid = dr["d_pid"].ToString();
          for(int j = 0; j < dt.Rows.Count; j++)
         {
          if(Convert.ToInt32(dt.Rows[j]["d_pid"]) == Convert.ToInt32(dt.Rows[i]["d_id"]))
            {
              TreeModel tm1 = new TreeModel();
               tm1.D_Cid = dr["d_cid"].ToString();
               tm1.D_Id = dr["d_id"].ToString();
               tm1.D_Name = dr["d_name"].ToString();
               tm1.D_Pid = dr["d_pid"].ToString();
               tm.TreeModels.add(tm1);
               for(int k = 0; k < dt.Rows.Count; k++)
               {
                 if(Convert.ToInt32(dt.Rows[k]["d_pid"]) == Convert.ToInt32(dt.Rows[j]["d_id"]))
                 {
                   TreeModel tm2 = new TreeModel();
                   tm2.D_Cid = dr["d_cid"].ToString();
                   tm2.D_Id = dr["d_id"].ToString();
                   tm2.D_Name = dr["d_name"].ToString();
                   tm2.D_Pid = dr["d_pid"].ToString();
                   tm1.TreeModels.add(tm2);
                   for(int m = 0; m< dt.Rows.Count; m++)
                   {
                   TreeModel tm3 = new TreeModel();
                   tm3.D_Cid = dr["d_cid"].ToString();
                   tm3.D_Id = dr["d_id"].ToString();
                   tm3.D_Name = dr["d_name"].ToString();
                   tm3.D_Pid = dr["d_pid"].ToString();
                   tm2.TreeModels.add(tm3);
                    }

                 }
               }
            }
         }
       list.add(tm);
        }
return list;
      }
    }
UI层
List<TreeModel> list=new GetAllTree();

foreach(TreeModel tm in list)
{
treeview.node.add(tm);
if(tm.TreeModels!=null)
{
foreach(TreeModel tm1 in tm.TreeModels)
tree.node.node.add(tm1);
if(tm1.TreeModels!=null)
{
foreach(TreeModel tm2 in tm1.TreeModels)
tree.node.node.node.add(tm2);
}
}
}

2012-11-18 02:20
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
得分:0 
如果不考虑性能的话,用递归一个方法简单搞定。
程序代码:
private void AddTree(IEnumerable<TreeModel> list, TreeNode parentNode, Int32 parentId)
        {
            var children = (from child in list
                            where child.D_Pid == parentId
                            select child);
            if (parentNode != null)
                foreach (var child in children)
                {
                    var treeNode = new TreeNode { Text = child.D_Name };
                    parentNode.Nodes.Add(treeNode);
                    AddTree(list, treeNode, child.D_Id);
                }
        }


用下列代码调用就可以了
List<TreeModel> trees = new TreeBLL().GetAllTree();
AddTree(trees, treeView1.Nodes, 0);

欢迎加入.Net技术交流群:90925122
2012-11-18 15:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-387363-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.469586 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved