How to Apply Filtering on Griddatabound Grid Based on a Node Selected in Tree View in Windows Forms GridDataBoundGrid(Classic)

28 Apr 2021 / 2 minutes to read

This page explains how to apply filter to GridDataBoundGrid based on Tree View selected node and more details.

How to Apply Filtering on GridDataBound Grid based on a Node Selected in Tree View?

GridDataBound grid is designed to bind data sources such as data table, string collection, data set, and data view. The filter bar can be enabled and hidden in the grid. Before selecting a node in the Tree view, the grid will load all the data from the data source. TreeView_AfterSelect event is used to determine the tree view node and perform filter operation on the grid control corresponding to the node that you selected in the Tree view. The following code example illustrates how to apply filtering on GridDataBound grid based on a node selected in the Tree view.

//Include the below four lines of code in the constructor
filter = new GridFilterBar();

//Associates Grid with the filter            

//Hides the filter bar row
this.gridDataBoundGrid1.Model.Rows.Hidden[1] = true;  

//Event Handler.
private void treeViewAdv1_AfterSelect(object sender, EventArgs e)
    TreeNodeAdv node = treeViewAdv1.SelectedNode;
    if (node != null)
        string filterString = string.Format("CategoryId = {0}",          Convert.ToInt32(node.Tag));

//Applies filter on grid.
filter.RowFilter = filterString;      
'Include the below four lines of code in the constructor

filter = New GridFilterBar()

'Associates Grid with the filter.            

'Hides the filter bar row.
Me.gridDataBoundGrid1.Model.Rows.Hidden(1) = True

'Event Handler
Private Sub treeViewAdv1_AfterSelect(ByVal sender As Object, ByVal e As EventArgs)
Dim node As TreeNodeAdv = treeViewAdv1.SelectedNode
If node IsNot Nothing Then
Dim filterString As String = String.Format("CategoryId = {0}", Convert.ToInt32(node.Tag))

'Applies filter on grid.
filter.RowFilter = filterString
End If
End Sub


A sample can be downloaded from the following location: