Devexpress Treelist 过滤的实现



Devexpress Treelist 的过滤行添加后,发现,只有父节点有这种信息,子节点才会显示。

可以毫不犹豫地说,这是一个bug.

但带来的影响还是很严重。


我用的版本是11.2


然后,到网上,找了一会。

果然许多老外也遇到这个问题。

其中,这几个链接:

http://www.devexpress.com/Support/Center/Question/Details/S139453

内容最多。

其中,有句:

TreeListOptionsFilter.FilterMode property to theFilterMode.Smart value

这个例子就是如此实现,http://www.devexpress.com/Support/Center/Question/Details/B212393


        private void Form1_Load(object sender, EventArgs e)
        {
            treeList1.OptionsBehavior.EnableFiltering = true;
            treeList1.OptionsView.ShowAutoFilterRow = true;
            treeList1.OptionsFilter.FilterMode = FilterMode.Smart;
            CreateTreeList();
        }


然而,同样是11.2,我所用的没有这个特性。

我看了13.2的,的确有这个特性:

Devexpress Treelist 过滤的实现_第1张图片

所以,还是希望找到一个简单的办法,

http://www.devexpress.com/Support/Center/Example/Details/E3072

这个链接事实上是上面那个给出来的

原文如下:

        private void Form1_Load(object sender, EventArgs e)
        {
            treeList1.DataSource = FillDataTable();
            treeList1.ExpandAll();
        }

        private void applyFilterButton_Click(object sender, EventArgs e)
        {
            FilterNodeOperation operation = new FilterNodeOperation(textEdit1.EditValue != null ? textEdit1.EditValue.ToString() : "");
            treeList1.NodesIterator.DoOperation(operation);
        }

        class FilterNodeOperation : 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 (TreeListColumn col in node.TreeList.Columns)
                    if (node.GetValue(col).ToString().Contains(pattern))
                        return true;
                return false;
            }
        }

我改了改:

        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();
                FilterNodeOperation operation = new FilterNodeOperation((!System.String.IsNullOrEmpty(newKey))?newKey : "");
                treeList1.NodesIterator.DoOperation(operation);
            }
            
        }  


终于可以了。

Devexpress Treelist 过滤的实现_第2张图片


对了,原始代码,有这几句话:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;





你可能感兴趣的:(filter,DevExpress,Treelist)