RadGrid for ASP.NET

Operating with the FilterExpression of Telerik RadGrid manually Send comments on this topic.
Filtering > How to > Operating with the FilterExpression of Telerik RadGrid manually

Glossary Item Box

The filter expressions in Telerik RadGrid can be "hooked" by two means:

  1. Through the ItemCommand event when e.CommandName is FilterCommandName. Then you can decide how the filtering data should be processed
  2. The second way is to use the NeedDataSource event where you can get all the filtering data from this property:

    RadGrid.MasterTableView.FilterExpression

    which is a string representing the filters the same way as to be used by DataView.Filter.

You can clear the GridTableView.FilterExpression string if you need to customize the filtering using custom statements.

Additionally, you can access the grid filter function/column unique name/search pattern for the filtered column when e.CommandName is RadGrid.FilterCommandName (inside the ItemCommand handler). This data is available through the e.CommandArgument (Pair object holding the filter function and the column unique name) and the GridFilteringItem (referencing the filter textbox text by the filtered column unique name).
Below is a sample code:

Example Title Copy Code
   protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
   {
       
if (e.CommandName == RadGrid.FilterCommandName)
       {
           Pair filterPair = (Pair)e.CommandArgument;
           gridMessage1 =
"Filter function chosen: " + filterPair.First + " for column '" + filterPair.Second + "'";
           
string filterPattern = ((TextBox)(e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0]).Text;
           gridMessage2 =
"<br> Entered pattern for search: " + filterPattern;
       }
   }
   
private string gridMessage1 = null, gridMessage2 = null;
   
protected void RadGrid1_DataBound(object sender, EventArgs e)
   {
       
if (!string.IsNullOrEmpty(gridMessage1))
       {
           DisplayMessage(gridMessage1);
           DisplayMessage(gridMessage2);
       }
   }
   
private void DisplayMessage(string text)
   {
       RadGrid1.Controls.Add(
new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
   }
VB .NET Copy Code
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs)
        If e.CommandName = RadGrid.FilterCommandName Then
            Dim filterPair As Pair = DirectCast(e.CommandArgument, Pair)
            gridMessage1 = "Filter function chosen: " + filterPair.First + " for column '" + filterPair.Second + "'"
            Dim filterPattern As String = (DirectCast((TryCast(e.Item, GridFilteringItem))(filterPair.Second.ToString()).Controls(0), TextBox)).Text
            gridMessage2 = "<br> Entered pattern for search: " + filterPattern
        End If
    End Sub

    Private gridMessage1 As String = Nothing, gridMessage2 As String = Nothing;
    Protected Sub RadGrid1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        If Not String.IsNullOrEmpty(gridMessage1) Then
            DisplayMessage(gridMessage1)
            DisplayMessage(gridMessage2)
        End If
    End Sub
    Private Sub DisplayMessage(ByVal text As String)
        RadGrid1.Controls.Add(New LiteralControl(String.Format("<span style='color:red'>{0}</span>", text)))
    End Sub