|
| 1 | +--- |
| 2 | +title: Filter with dynamically created filter expressions |
| 3 | +description: This article provides instructions on how to filter with dynamically created filter expressions |
| 4 | +type: how-to |
| 5 | +page_title: Filter with dynamically created filter expressions. | RadGrid |
| 6 | +slug: grid-filter-with-dynamically-created-filter-expressions |
| 7 | +tags: radgrid, filtering, buttons, rows |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Telerik WebForms Grid for ASP.NET AJAX</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +I want to implement buttons to filter rows in a step-by-step process. The process involves listing and filtering rows in a Grid based on specific intervals. |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To achieve the desired behavior, you can follow these steps: |
| 29 | + |
| 30 | +1. Add a Filter control with the `Visible` property set to **false** to your page. |
| 31 | +2. Create buttons in the header (or wherever you want) of a GridTemplateColumn in your RadGrid to trigger the filtering. |
| 32 | +3. Handle the button click events and programmatically create filter expressions based on the given conditions. |
| 33 | +4. Apply the filter expressions to the Grid and remove them after filtering is done. |
| 34 | + |
| 35 | +Here's a sample implementation for the first scenario using buttons in the header of a GridTemplateColumn: |
| 36 | + |
| 37 | +````ASP.NET |
| 38 | +<telerik:RadFilter RenderMode="Lightweight" runat="server" ID="RadFilter1" FilterContainerID="RadGrid1" Visible="false" /> |
| 39 | +<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource"> |
| 40 | + <MasterTableView AutoGenerateColumns="true" DataKeyNames="OrderID"> |
| 41 | + <Columns> |
| 42 | + <telerik:GridTemplateColumn DataField="OrderID" DataType="System.Int32" FilterControlAltText="Filter OrderID column" HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID"> |
| 43 | + <HeaderTemplate> |
| 44 | + <telerik:RadButton runat="server" ID="Step1Button" Text="Step1-button" OnClick="Step1Button_Click" /> |
| 45 | + <telerik:RadButton runat="server" ID="Step2Button" Text="Step2-button" OnClick="Step2Button_Click" /> |
| 46 | + </HeaderTemplate> |
| 47 | + <ItemTemplate> |
| 48 | + <telerik:RadLabel runat="server" ID="label1" Text='<%# Bind("OrderID") %>' /> |
| 49 | + </ItemTemplate> |
| 50 | + </telerik:GridTemplateColumn> |
| 51 | + </Columns> |
| 52 | + </MasterTableView> |
| 53 | +</telerik:RadGrid> |
| 54 | +```` |
| 55 | + |
| 56 | +In the code-behind file, handle the button click events and create the filter expressions: |
| 57 | + |
| 58 | +````C# |
| 59 | +protected void Step1Button_Click(object sender, EventArgs e) |
| 60 | +{ |
| 61 | + RadFilterGreaterThanOrEqualToFilterExpression<int> expr1 = new RadFilterGreaterThanOrEqualToFilterExpression<int>("OrderID"); |
| 62 | + expr1.Value = 1; |
| 63 | + RadFilter1.RootGroup.AddExpression(expr1); |
| 64 | + |
| 65 | + RadFilterGroupExpression group1 = new RadFilterGroupExpression(); |
| 66 | + group1.GroupOperation = RadFilterGroupOperation.And; |
| 67 | + |
| 68 | + RadFilterLessThanOrEqualToFilterExpression<int> expr2 = new RadFilterLessThanOrEqualToFilterExpression<int>("OrderID"); |
| 69 | + expr2.Value = 61; |
| 70 | + RadFilter1.RootGroup.AddExpression(expr2); |
| 71 | + |
| 72 | + RadFilter1.FireApplyCommand(); |
| 73 | + |
| 74 | + RadFilter1.RootGroup.Expressions.Remove(expr1); |
| 75 | + RadFilter1.RootGroup.Expressions.Remove(expr2); |
| 76 | +} |
| 77 | + |
| 78 | +protected void Step2Button_Click(object sender, EventArgs e) |
| 79 | +{ |
| 80 | + RadFilterGreaterThanOrEqualToFilterExpression<int> expr1 = new RadFilterGreaterThanOrEqualToFilterExpression<int>("OrderID"); |
| 81 | + expr1.Value = 61; |
| 82 | + RadFilter1.RootGroup.AddExpression(expr1); |
| 83 | + |
| 84 | + RadFilterGroupExpression group1 = new RadFilterGroupExpression(); |
| 85 | + group1.GroupOperation = RadFilterGroupOperation.And; |
| 86 | + |
| 87 | + RadFilterLessThanOrEqualToFilterExpression<int> expr2 = new RadFilterLessThanOrEqualToFilterExpression<int>("OrderID"); |
| 88 | + expr2.Value = 150; |
| 89 | + RadFilter1.RootGroup.AddExpression(expr2); |
| 90 | + |
| 91 | + RadFilter1.FireApplyCommand(); |
| 92 | + |
| 93 | + RadFilter1.RootGroup.Expressions.Remove(expr1); |
| 94 | + RadFilter1.RootGroup.Expressions.Remove(expr2); |
| 95 | +} |
| 96 | +```` |
| 97 | + |
| 98 | +This implementation allows you to filter the Grid based on the given conditions by clicking the corresponding buttons in the header. |
| 99 | + |
| 100 | +Remember to adjust the column and filter expressions based on your specific requirements. |
| 101 | + |
| 102 | +## See Also |
| 103 | + |
| 104 | +- [Working with Expressions]({%slug filter/filter-expressions/working-with-expressions%}) |
| 105 | + |
| 106 | + |
0 commit comments