Remove the filter for a single RadGrid column

Chris B picture Chris B · Oct 16, 2012 · Viewed 10k times · Source

I am using a Telerik RadGrid and have custom filters set up on some of the columns. As part of that custom filter (which is just a textbox) I also have a button that when clicked I want to clear the filter value for that particular column only.

I have the button working and am setting the filter for the particular column to an empty string, then rebinding the grid but this does not remove the filter value.

It seems that only modifying the grid.MasterTableView.FilterExpression actually affects the filter but how can I remove just one column's filter values from the FilterExpression?

<telerik:RadTextBox ID="RdTxtC" EmptyMessage="Search" runat="server" EmptyMessageStyle-Font-Italic="true"
    CssClass="padtop4" HoveredStyle-Font-Italic="true" ClientEvents-OnKeyPress="filterC"
    InputType="Text" Width="80%">
</telerik:RadTextBox>
<asp:Button ID="ibtnClearFilterTown" ClientIDMode="Static" runat="server" CssClass="clearFilterButton" OnClick="ibtnClearFilterTown_Click"/>

In the code-behind:

protected void ibtnClearFilterTown_Click(object sender, EventArgs e)
{
    RgClientList.MasterTableView.GetColumnSafe("TownCity").CurrentFilterValue = String.Empty;
    RgClientList.Rebind();
}

It seems a fairly simple requirement to remove the filter for a single column so i think I must be missing something obvious - any help is appreciated....

Answer

KreepN picture KreepN · Oct 16, 2012

Go ahead and try this instead of what you have in your code:

protected void ibtnClearFilterTown_Click(object sender, EventArgs e)
{
   RgClientList.MasterTableView.GetColumnSafe("TownCity").CurrentFilterFunction = GridKnownFunction.NoFilter;
   RgClientList.MasterTableView.GetColumnSafe("TownCity").CurrentFilterValue = String.Empty;
   RgClientList.Rebind();

}

Also, according to this question asked on SO, you may need to change the following property on your Radgrid:

EnableLinqExpressions="False"

EDIT 1: (put this in the button press)

I understand this looks like a lot to do what you wish, but I checked the documentation for Telerik and did not see an easy way of simply removing one filter string from the MasterTableView.FilterExpression. This is mainly due to the way each column stores its filter as text and not the value that goes into the final filter string.

I'll walk you through the steps involved below:

  • It creates a collection of filters from each column, removes the one you do not want (columnwhosfilteryouwantremoved), and then loops through the columns and clears the column you wish to set = ""
  • Then it sets the the filter for the grid to its new value and then rebinds.

    String MasterFilterExp = "";        
    
    var tempCollection = (from String sFilt in RadGrid1.MasterTableView.FilterExpression.Split(new string[] {"AND"},StringSplitOptions.None)
                       where sFilt.Contains("[columnnameoffilteryouwantremoved]") != true 
                       select sFilt).ToArray();
    
    MasterFilterExp = string.Join(" AND ", tempCollection);
    
    foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
    {
        if (column.UniqueName == "columnnameoffilteryouwantremoved")
        {                
            column.CurrentFilterFunction = GridKnownFunction.NoFilter;
            column.CurrentFilterValue = String.Empty;
        }            
    }
    
    RadGrid1.MasterTableView.FilterExpression = MasterFilterExp;
    RadGrid1.Rebind();                        
    

It can probably be simplified, but let me know if you have any questions. I tested it and was able to remove a column's filter from the grid and rebind to show the updated info.