Filtering in Windows Forms MultiColumn TreeView

28 Apr 2021 / 3 minutes to read

Filtering is the process of retrieving the values from a collection that satisfies the specified condition.

Filter Level

You can filter the nodes based on level using the FilterLevel property.

this.multiColumnTreeView1.FilterLevel = FilterLevel.All;
Me.multiColumnTreeView1.FilterLevel = FilterLevel.All
  • Root - Filter will be applied only to root nodes in MultiColumnTreeView. All other nodes will be invisible in view.

  • All - Filter will be applied to all the nodes in MultiColumnTreeView. If a parent node does not match the filter condition, filter will not be applied for child nodes. Else, filter will be applied to its child nodes also.

  • Extended - Filter will be applied to all the nodes in MultiColumnTreeView. If a node matches the filter condition, its all ancestors will also be displayed even though the parent node does not match the filter condition.

NOTE

You can change the FilterLevel at run time.

Apply filter

Filtering can be achieved by setting the Filter delegate and calling the RefreshFilter method.

public bool FilterNodes(object o)
{
  var nodeadv = o as TreeNodeAdv;
  int value = int.Parse(nodeadv.SubItems[2].Text.ToString());
  if (value > 23000)
    return true;
  return false;
}

private void button1_Click(object sender, EventArgs e)
{
  if (multiColumnTreeView1 != null)
  {
    multiColumnTreeView1.Filter = FilterNodes;
    multiColumnTreeView1.RefreshFilter();
  }
}
Public Function FilterNodes(ByVal o As Object) As Boolean
  Dim nodeadv = TryCast(o, TreeNodeAdv)
  Dim value As Integer = Integer.Parse(nodeadv.SubItems(2).Text.ToString())
  If value > 23000 Then Return True
  Return False
End Function 

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
  If multiColumnTreeView1 IsNot Nothing Then
    multiColumnTreeView1.Filter = Nothing
    multiColumnTreeView1.RefreshFilter()
  End If
End Sub

Here, the FilterNodes delegate filters the node based on Salary. The FilterNodes delegate is assigned to  Filter predicate to filter the nodes. After that, RefreshFilter method is called to refresh the nodes.

WindowsForms MultiColumnTreeView Filtering

Clear filter

You can clear the filters applied in nodes by setting the Filter delegate to null and calling the RefreshFilter method.

multiColumnTreeView1.Filter = null;
multiColumnTreeView1.RefreshFilter();
multiColumnTreeView1.Filter = Nothing
multiColumnTreeView1.RefreshFilter()

NOTE

View sample in GitHub.