一种加载TreeView数据的新写法

今天在MSDN看到一篇文章,受到启发,便用下面的方式实现TreeView的数据加载。因为使用了spring来注入业务方法,所以不完全贴上代码了,只贴上关键部分。
        /// <summary>
        
/// 执行数据显示
        
/// </summary>
        public void showTree()
        {
            image1 
= Resources.home.ToBitmap();
            image2 
= Resources.department.ToBitmap();
            image3 
= Resources.users.ToBitmap();

            imageList.Images.Add(image1);
            imageList.Images.Add(image2);
            imageList.Images.Add(image3);

            
if (treeDataSupportImpl == null)
            {
                treeDataSupportImpl 
= (ITreeDataSupport)AppUtils.getObject("treeDataSupportImpl");
            }

            
// 获取数据
            IList treeDataList= treeDataSupportImpl.GetTreeData(falsefalse);
            IList empOrPosList 
= new List<IDictionary>();
            
if (isLoadEmployee || isLoadPosition)
            {
                empOrPosList 
= treeDataSupportImpl.GetTreeData(isLoadEmployee, isLoadPosition);
            }
            log.Debug(treeDataList.Count);
            
if (treeDataList.Count > 0 && treeDataList != null)
            {
                
// 转换为DataTable
                this.converListToDataTable(treeDataList);

                
// 调用数据展现方法
                log.Debug("开始创建、、、");
                
this.createTree(0,(TreeNode)null);

                
// 如果有数,则加载
                if (empOrPosList.Count > 0 && empOrPosList != null)
                {
                    
this.addTree(empOrPosList);
                }

            }
        }

        
/// <summary>
        
/// 添加员工或职称到树
        
/// </summary>
        
/// <param name="list"></param>
        public void addTree(IList list)
        {
            
foreach (IDictionary empOrPos in list)
            {
                TreeNode newNode 
= new TreeNode();
                newNode.Name 
= empOrPos["ORG_ID"].ToString();
                newNode.Text 
= empOrPos["ORG_NAME"].ToString();
                newNode.ImageIndex 
= 2;
                newNode.SelectedImageIndex 
= 2;
                TreeNode node 
= myTreeView.Nodes.Find(empOrPos["SUPER_ORG_ID"].ToString(), true)[0];
                node.Nodes.Add(newNode);
            }
        }

        
/// <summary>
        
/// 创建树
        
/// </summary>
        
/// <param name="ParentID"></param>
        
/// <param name="pNode"></param>
        public void createTree(int ParentID,TreeNode pNode)
        {
            DataView dvTree 
= new DataView(treeData);
            dvTree.RowFilter 
= "[SUPER_ORG_ID] = " + ParentID;

            
foreach (DataRowView Row in dvTree)
            {
                
if(pNode == null)
                {
                    log.Debug(
"根节点:" + Row["ORG_NAME"].ToString());
                    
// 添加根节点
                    myTreeView.ImageList = imageList;
                    TreeNode node 
= myTreeView.Nodes.Add(Row["ORG_NAME"].ToString());
                    node.Name 
= Row["ORG_ID"].ToString();
                    node.ImageIndex 
= 0;
                    node.SelectedImageIndex 
= 0;
                    
// 递归
                    createTree(Int32.Parse(Row["ORG_ID"].ToString()),node);    
                }
                
else
                {
                    log.Debug(
"节点:" + Row["ORG_NAME"].ToString());
                    
// 添加当前节点
                    TreeNode node =  pNode.Nodes.Add(Row["ORG_NAME"].ToString());
                    node.Name 
= Row["ORG_ID"].ToString();
                    
if(Row["ORG_TYPE_ID"].ToString().Equals("2"|| Row["ORG_TYPE_ID"].ToString().Equals("3"))
                    {
                        node.ImageIndex 
= 1;
                        node.SelectedImageIndex 
= 1;
                    }
                    
else
                    {
                        node.ImageIndex 
= 2;
                        node.SelectedImageIndex 
= 2;
                    }

                    
//再次递归
                    createTree(Int32.Parse(Row["ORG_ID"].ToString()),node);
                }
            }

        }

        
/// <summary>
        
/// 转换函数
        
/// </summary>
        
/// <param name="list"></param>
        public void converListToDataTable(IList list)
        {
            DataColumn dataColumn 
= treeData.Columns.Add("ORG_ID"typeof(Int32));
            dataColumn.AllowDBNull 
= false;
            treeData.Columns.Add(
"ORG_NAME"typeof(String));
            treeData.Columns.Add(
"SUPER_ORG_ID"typeof(Int32));
            treeData.Columns.Add(
"ORG_TYPE_ID"typeof(Int32));

            
foreach (IDictionary dict in list)
            {
                DataRow row 
= treeData.NewRow();
                row[
"ORG_ID"= dict["ORG_ID"];
                row[
"ORG_NAME"= dict["ORG_NAME"];
                row[
"SUPER_ORG_ID"= dict["SUPER_ORG_ID"];
                row[
"ORG_TYPE_ID"= dict["ORG_TYPE_ID"];
                treeData.Rows.Add(row);
            }
            
/*
            foreach (DataRow row in treeData.Rows)
            {
                log.Debug(row["ORG_ID"].ToString() + " | " + row["ORG_NAME"].ToString() + " | " + row["SUPER_ORG_ID"].ToString() + " | " + row["ORG_TYPE_ID"].ToString());
            }
             * 
*/
        }

posted @ 2007-01-23 23:00  电视机9号  阅读(...)  评论(...编辑  收藏