c# Treeview过滤和绑定

纵饮孤独 提交于 2019-12-01 01:52:44
        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="str"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private IList<CV_QM_DEFECT_LABEL_GROUP> onFindKeyWord(string str, IList<CV_QM_DEFECT_LABEL_GROUP> list)
        {

            List<CV_QM_DEFECT_LABEL_GROUP> m_list = new List<CV_QM_DEFECT_LABEL_GROUP>();
            foreach (CV_QM_DEFECT_LABEL_GROUP data in list)
            {
                if (data.GroupName.IndexOf(str) != -1)
                {
                    m_list.Add(data);
                }
            }
            return m_list;

        }
        /// <summary>
        /// 寻找所有相关根节点
        /// </summary>
        private List<CV_QM_DEFECT_LABEL_GROUP> findparent()
        {
            IList<CV_QM_DEFECT_LABEL_GROUP> entities = this.icv_QM_DEFECT_LABEL_GROUPBO_0.GetEntities();
            IList<CV_QM_DEFECT_LABEL_GROUP> filterdata = onFindKeyWord(txtquery.Text.Trim(), entities);
            List<CV_QM_DEFECT_LABEL_GROUP> entities1 = new List<CV_QM_DEFECT_LABEL_GROUP>();//转化为list方便使用方法
            List<CV_QM_DEFECT_LABEL_GROUP> entities2 = new List<CV_QM_DEFECT_LABEL_GROUP>();//用于存放所有节点

            foreach (var item in (IList<CV_QM_DEFECT_LABEL_GROUP>)entities)
            {
                entities1.Add(item);
            }
            foreach (CV_QM_DEFECT_LABEL_GROUP data in filterdata)
            {
                CV_QM_DEFECT_LABEL_GROUP temp1 = data;
                while (true)
                {
                    if (temp1.ParentGroupID.HasValue)
                    {
                        if (entities2.Find(s => s.GroupID.Equals(temp1.GroupID)) == null)
                        { entities2.Add(temp1); }
                        CV_QM_DEFECT_LABEL_GROUP temp = entities1.Find(s => s.GroupID.Equals(temp1.ParentGroupID));
                        temp1 = temp;
                    }
                    else
                    {
                        if (entities2.Find(s => s.GroupID.Equals(temp1.GroupID)) == null)
                        { entities2.Add(temp1); }
                        break;
                    }
                }
            }
            return entities2.OrderBy(item => item.GroupLevel).ToList();
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void bind()
        {
            IList<CV_QM_DEFECT_LABEL_GROUP> entities = findparent();
            this.TreeView1.Nodes.Clear();
            foreach (CV_QM_DEFECT_LABEL_GROUP cv_qm_defect_label_group in entities)
            {
                if (cv_qm_defect_label_group.ParentGroupID.HasValue)
                {
                    TreeNode[] nodeArray = this.TreeView1.Nodes.Find(cv_qm_defect_label_group.ParentGroupID.Value.ToString(), true);
                    if ((nodeArray != null) && (nodeArray.Length > 0))
                    {
                        nodeArray[0].Nodes.Add(cv_qm_defect_label_group.GroupID.Value.ToString(), cv_qm_defect_label_group.GroupName, "Group.png", "Group.png");
                        nodeArray[0].Nodes[cv_qm_defect_label_group.GroupID.Value.ToString()].Tag = cv_qm_defect_label_group;
                    }
                    else
                    {
                        this.TreeView1.Nodes.Add(cv_qm_defect_label_group.GroupID.Value.ToString(), cv_qm_defect_label_group.GroupName, "Group.png", "Group.png");
                        this.TreeView1.Nodes[cv_qm_defect_label_group.GroupID.Value.ToString()].Tag = cv_qm_defect_label_group;
                    }
                }
                else
                {
                    this.TreeView1.Nodes.Add(cv_qm_defect_label_group.GroupID.Value.ToString(), cv_qm_defect_label_group.GroupName, "Group.png", "Group.png");
                    this.TreeView1.Nodes[cv_qm_defect_label_group.GroupID.Value.ToString()].Tag = cv_qm_defect_label_group;
                }
            }
            this.TreeView1.ExpandAll();
        }

    }
}

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!