DevExpress控件-TreeList内置过滤功能实现(图文)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq395537505/article/details/51314974

以下效果TreeList必须先绑定数据源,绑定方法如下:

 this.treeList1.KeyFieldName = "id";
 this.treeList1.ParentFieldName = "parentId";
 this.treeList1.DataSource = ds.Tables[0];

效果:

实现代码如下:

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //显示过滤行
            treeList1.OptionsView.ShowAutoFilterRow = true;
            //开启过滤功能
            treeList1.OptionsBehavior.EnableFiltering = true;
            //定义TreeList列过滤事件
            treeList1.ColumnFilterChanged += TreeList1_ColumnFilterChanged;
        }
        /// <summary>
        /// 列过滤事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeList1_ColumnFilterChanged(object sender, EventArgs e)
        {
            //DevExpress.XtraTreeList.Nodes.TreeListNode fNode = treeList1.FocusedNode;
            //DevExpress.XtraTreeList.Columns.TreeListColumn fColumn = treeList1.FocusedColumn;
            if (treeList1.ActiveEditor != null)
            {
                string newKey = treeList1.ActiveEditor.EditValue.ToString();
                treeList1.FilterNodes();
                FilterNodeOperation operation = new FilterNodeOperation((!System.String.IsNullOrEmpty(newKey)) ? newKey : "");
                treeList1.NodesIterator.DoOperation(operation);
            }
        }
        /// <summary>
        /// 过滤节点操作类
        /// </summary>
        class FilterNodeOperation : DevExpress.XtraTreeList.Nodes.Operations.TreeListOperation
        {
            string pattern;
            public FilterNodeOperation(string _pattern)
            { pattern = _pattern; }
            public override void Execute(TreeListNode node)
            {
                if (NodeContainsPattern(node, pattern))
                {
                    node.Visible = true;
                    if (node.ParentNode != null)
                        node.ParentNode.Visible = true;
                }
                else
                    node.Visible = false;
            }
            bool NodeContainsPattern(TreeListNode node, string pattern)
            {
                foreach (DevExpress.XtraTreeList.Columns.TreeListColumn col in node.TreeList.Columns)
                    if (node.GetValue(col).ToString().ToUpper().Contains(pattern.ToUpper()))
                        return true;
                return false;
            }
        }


展开阅读全文

没有更多推荐了,返回首页