Category / Section
How to filter a column based on the value of the clicked cell?
5 mins read
By default, the filtering operation is performed only by selecting a value through the filter bar dropdown. To filter a column based on the value of the clicked cell and to apply that value in the filter bar cell, you can customize the CellClick event and filter the cell value by using RowFilter. You can assign the display value of the filter bar cell through the CellValue property of that cell.
C#
this.gridDataBoundGrid1.CellClick += new GridCellClickEventHandler(gridDataBoundGrid1_CellClick); void gridDataBoundGrid1_CellClick(object sender, GridCellClickEventArgs e) { if (this.gridDataBoundGrid1[e.RowIndex, e.ColIndex] != null) { filtervalue = this.gridDataBoundGrid1[e.RowIndex, e.ColIndex].CellValue.ToString(); string colname=this.gridDataBoundGrid1.Model[0,e.ColIndex].CellValue.ToString(); if (colname != null) { //display the filtered rows this.filterbar.RowFilter = "" + colname + "='" + filtervalue+"'"; } } //display the filtered value in the fiterbar this.gridDataBoundGrid1[1, e.ColIndex].CellValue = filtervalue; }
VB
Private Me.gridDataBoundGrid1.CellClick += New GridCellClickEventHandler(AddressOf gridDataBoundGrid1_CellClick) Private Sub gridDataBoundGrid1_CellClick(ByVal sender As Object, ByVal e As GridCellClickEventArgs) If Me.gridDataBoundGrid1(e.RowIndex, e.ColIndex) IsNot Nothing Then filtervalue = Me.gridDataBoundGrid1(e.RowIndex, e.ColIndex).CellValue.ToString() Dim colname As String=Me.gridDataBoundGrid1.Model(0,e.ColIndex).CellValue.ToString() If colname IsNot Nothing Then 'display the filtered rows Me.filterbar.RowFilter = "" & colname & "='" & filtervalue & "'" End If End If 'display the filtered value in the fiterbar Me.gridDataBoundGrid1(1, e.ColIndex).CellValue = filtervalue End Sub
The filter value is displayed in the filter bar as illustrated in the following screenshot.
Figure 1: Filter value is displayed in the filter bar
Sample link
C#: Set Filtervalue
VB: Set Filtervalue