欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

关于 DevExpress.XtraTreeList.TreeList 树形控件 的操作-树形节点的查找:

最编程 2024-07-31 19:08:54
...

效果图是最开始的第二张图

代码:

/// <summary>
        /// 搜索事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
                List<TreeListModel> list = new List<TreeListModel>();
                List<int> listId = new List<int>();//这个集合用于存储已经加入了的父节点ID,因为有些子节点可能同在一个父节点下若重复插入同ID的数据会报错
                string inputStr = this.txtModelName.Text.Trim();
                string systemId = string.Empty;
                if (this.cmbSystemClassify.EditValue != null)
                {
                    systemId = this.cmbSystemClassify.EditValue.ToString();
                }
                else
                {
                    systemId = "0";
                }
                if (string.IsNullOrEmpty(inputStr))
                {
                    //如果为空则查询所有
                    List<BW.DMSystem.Core.Entity.ModelManageInfo> temp = BLLFactory<BW.DMSystem.Core.BLL.ModelManage>.Instance.GetAll(string.Format("where IsGallery = 0 and SystemID = '{0}' order by ID asc", systemId));
                    TreeList(0, ref list, temp);
                }
                else
                {
                    //查询出所有数据
                    List<BW.DMSystem.Core.Entity.ModelManageInfo> allData = BLLFactory<BW.DMSystem.Core.BLL.ModelManage>.Instance.GetAll(string.Format("where IsGallery = 0 and SystemID = '{0}' order by ID asc", systemId));
                    //查询出满足条件的
                    List<BW.DMSystem.Core.Entity.ModelManageInfo> temp = BLLFactory<BW.DMSystem.Core.BLL.ModelManage>.Instance.GetAll(string.Format("where IsGallery = 0 and IsModel = 1 and SystemID = '{0}' and Name like '%{1}%' order by ID asc", systemId, inputStr));
                    for (int i = 0; i < temp.Count; i++)
                    {
                        TreeListModel currNode = new TreeListModel();
                        currNode.ID = temp[i].ID;
                        currNode.MenuName = temp[i].Name;
                        currNode.ParentID = temp[i].ParentCode;
                        currNode.Tag = temp[i];
                        list.Add(currNode);
                        listId.Add(temp[i].ID);
                        TreeList_fanxiang(temp[i].ParentCode, ref list, ref listId, allData);
                    }
                }
                this.tvFacilityModel.KeyFieldName = "ID";
                this.tvFacilityModel.ParentFieldName = "ParentID";
                this.tvFacilityModel.DataSource = list;
                this.tvFacilityModel.ExpandAll();
            }
        }

 

/// <summary>
        /// 递归调用——反向查找节点的根节点
        /// </summary>
        /// <param name="currId">根节点的父编码默认为0</param>
        /// <param name="listData">树形绑定的数据集合</param>
        /// <param name="modelList">某系统的结构数据</param>
        private void TreeList_fanxiang(int parentId, ref List<TreeListModel> listData, ref List<int> listId, List<BW.DMSystem.Core.Entity.ModelManageInfo> modelList)
        {
            List<BW.DMSystem.Core.Entity.ModelManageInfo> tmp = modelList.Where(model => model.ID == parentId).ToList();//正向和反向的最终区别就在于这句话
            foreach (var item in tmp)
            {
                TreeListModel temp = new TreeListModel();
                temp.ID = item.ID;
                temp.MenuName = item.Name;
                temp.ParentID = item.ParentCode;
                temp.Tag = item;
                if (!listId.Contains(item.ID))
                {
                    listData.Add(temp);
                    listId.Add(item.ID);
                }
                TreeList_fanxiang(item.ParentCode, ref listData, ref listId, modelList);
            }            
        }

 

 根据注释理解代码的含义,节点查找也告一段落了。