简体   繁体   中英

Remove the filter for a single RadGrid column

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....

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.

Why dont you try it with Javacript:

function FilterMenuShowing(sender, eventArgs)   
   {     
       if (eventArgs.get_column().get_uniqueName() == "IsPostable")      
       {              
           var menu = eventArgs.get_menu();   
           var items = menu._itemData;   

           var i = 0;   

           while (i < items.length)     
           {     
                   var item = menu._findItemByValue(items[i].value);   
                   if (item != null)   
                       item._element.style.display="none";   
               i++;     
           }
       }
 }

You can see details here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM