Search code examples
c#objectlistviewtreelistview

ObjectListView advanced search and filtering


I'm trying to search and filter results on a TreeListView object from the ObjectListView component. Currently, I'm implementing this into a C# (.NET 4.0) project which have the following classes

MyAbstract, MyDir (inherit MyAbstract) and MyFile (inherit MyAbstract as well). These classes have the following properties: Name, Title, Speed, SpeedType.

I want to know how to correctly create a query-like filter to this list, such as for example:

Speed < 10 OR SpeedType == "RPM"

I probably might use LINQ to it, but my main problem is how to apply and manage this using the TreeListView. My main questions are:

  1. How to create this kind of filtering on the TreeListView?

  2. How to make the TreeListView display only the filtered results

  3. How to make it save the original list to have a clear filter button.

This is how I currently setup my list:

public void Init()
{
    Project.LoadDirectory();

    treeListView1.SetObjects(new object[] { Project.Root });

    treeListView1.CanExpandGetter = delegate(object x)
    {
        return (x is MyDir);
    };

    treeListView1.ChildrenGetter = delegate(object x)
    {
        return ((MyDir)x).Nodes;
    };

    olvColumn1.ImageGetter = new ImageGetterDelegate(this.TreeViewImageGetter);
}

I've looked over the documentation but it stills unclear to me.


Solution

  • What have you tried?

    This will filter the TreeListView to only show MyFile objects that match the condition you gave in your question:

    this.treeListView.ModelFilter = new ModelFilter(delegate(object x) {
        var myFile = x as MyFile;
        return x != null && (myFile.Speed < 10 || myFile.SpeedType == "RPM");
    });
    

    To stop filtering, just clear the file again:

    this.treeListView.ModelFilter = null;
    

    The demo that comes with the project shows all this behaviour.