|
| 1 | +--- |
| 2 | +title: Display Active Filters Above Grid |
| 3 | +description: Learn how to show which columns are being filtered in a RadGrid control and the specific filter values applied to each column. |
| 4 | +type: how-to |
| 5 | +page_title: Display Active Filters Above Grid |
| 6 | +slug: grid-display-active-filters-above-grid |
| 7 | +tags: radgrid, asp.net ajax, filtering, display, columns, values |
| 8 | +res_type: kb |
| 9 | +ticketid: 1672360 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadGrid for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>All</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +I need to display the active filters applied to a RadGrid control, including both the columns being filtered and the specific values used for filtering. The filtered columns and their respective values should be shown in a label above the control. |
| 30 | + |
| 31 | +This knowledge base article also answers the following questions: |
| 32 | + |
| 33 | +- How can I display the columns that are currently being filtered in a RadGrid? |
| 34 | +- How do I show the filter values for each filtered column in a RadGrid? |
| 35 | +- What method can be used to list active filters in a RadGrid control? |
| 36 | + |
| 37 | +## Solution |
| 38 | + |
| 39 | +To display the filtered columns and their corresponding filter values above a RadGrid control, follow these steps: |
| 40 | + |
| 41 | +1. Loop through each column in the RadGrid to check if it is being filtered. |
| 42 | +2. For columns with active filters, retrieve the filter value and function. |
| 43 | +3. Concatenate the collected filter details into a readable format. |
| 44 | +4. Display this information in a label or another suitable control placed above the RadGrid. |
| 45 | + |
| 46 | +Here is a code example that demonstrates this approach: |
| 47 | + |
| 48 | +````ASP.NET |
| 49 | +<asp:Label Text="lblFilterDetails" ID="lblFilterDetails" runat="server" /> |
| 50 | +<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnPreRender="RadGrid1_PreRender" |
| 51 | + AllowFilteringByColumn="true" OnNeedDataSource="RadGrid1_NeedDataSource"> |
| 52 | + <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID"> |
| 53 | + <Columns> |
| 54 | + <!-- Define your RadGrid columns here --> |
| 55 | + </Columns> |
| 56 | + </MasterTableView> |
| 57 | +</telerik:RadGrid> |
| 58 | +```` |
| 59 | + |
| 60 | +````C# |
| 61 | +protected void RadGrid1_PreRender(object sender, EventArgs e) |
| 62 | +{ |
| 63 | + List<string> filterDetails = new List<string>(); |
| 64 | + |
| 65 | + foreach (GridColumn column in RadGrid1.MasterTableView.RenderColumns) |
| 66 | + { |
| 67 | + if (!string.IsNullOrEmpty(column.CurrentFilterValue)) |
| 68 | + { |
| 69 | + string filteredColumn = column.UniqueName; |
| 70 | + string filterFunction = column.CurrentFilterFunction.ToString(); |
| 71 | + string filterValue = column.CurrentFilterValue; |
| 72 | + |
| 73 | + string filterInfo = string.Format("Filtering column {0} with filter function {1} and value {2}", filteredColumn, filterFunction, filterValue); |
| 74 | + |
| 75 | + filterDetails.Add(filterInfo); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + lblFilterDetails.Text = filterDetails.Count > 0 ? "Filtered Columns: " + string.Join(", ", filterDetails) : "No filters applied."; |
| 80 | +} |
| 81 | +```` |
| 82 | + |
| 83 | +### Explanation |
| 84 | +- **RadGrid1_PreRender**: This event ensures that the filter information is retrieved after filtering is applied. |
| 85 | +- **filterDetails**: A list that holds the filter details for each filtered column. |
| 86 | +- **lblFilterDetails**: The control used to display the active filter information above the RadGrid. |
| 87 | + |
| 88 | +By following this solution, you can effectively display the filtered columns along with their filter functions and values, helping users understand how the data is being filtered. |
| 89 | + |
| 90 | +## See Also |
| 91 | + |
| 92 | +- [RadGrid Filtering Overview](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/functionality/filtering/overview) |
0 commit comments