Multiple filter groups on radgrid

3 posts, 0 answers
  1. adam
    adam avatar
    15 posts
    Member since:
    Sep 2010

    Posted 20 Oct 2010 Link to this post

    i have a radgrid which i want to filter using the radfilter control.

    it kind of works but i guess i'm missing something.

    i have a textbox which filter three columns of the radgrid, i then have two radcomboboxes that filter a column each.

    i want to combine these filters together so the user can filter down to the record they want.

    protected void Filter_Click(object sender, EventArgs e)
        {
            if (QuickSearchBox.Text != "")
            {
                RadFilterContainsFilterExpression expression1 = new RadFilterContainsFilterExpression("Synopsis");
                SearchRadFilter.RootGroup.AddExpression(expression1);
     
                SearchRadFilter.RootGroup.GroupOperation = RadFilterGroupOperation.Or;
     
                RadFilterContainsFilterExpression expression2 = new RadFilterContainsFilterExpression("Title");
                SearchRadFilter.RootGroup.AddExpression(expression2);
     
                SearchRadFilter.RootGroup.GroupOperation = RadFilterGroupOperation.Or;
     
                RadFilterContainsFilterExpression expression3 = new RadFilterContainsFilterExpression("Text");
                SearchRadFilter.RootGroup.AddExpression(expression3);
     
                expression1.Value = QuickSearchBox.Text;
                expression2.Value = QuickSearchBox.Text;
                expression3.Value = QuickSearchBox.Text;
            }
     
             
            RadFilterGroupExpression group = new RadFilterGroupExpression();
            group.GroupOperation = RadFilterGroupOperation.And;
            RadFilterContainsFilterExpression expr = new RadFilterContainsFilterExpression("ClassDisplayName");
            expr.Value = TypeFilter.SelectedValue;
            SearchRadFilter.RootGroup.AddExpression(group);
            group.AddExpression(expr);
     
            RadFilterGroupExpression group2 = new RadFilterGroupExpression();
            group2.GroupOperation = RadFilterGroupOperation.And;
            RadFilterContainsFilterExpression expr2 = new RadFilterContainsFilterExpression("NodeParentID");
            expr2.Value = NodeValue.Value;
            SearchRadFilter.RootGroup.AddExpression(group2);
            group.AddExpression(expr2);
             
            SearchRadFilter.FireApplyCommand();     
        }

    i'm not getting any errrors, but if you use the textbox and the classdisplayname filter i won't it to come back with the textsearch but only display the records in that class. at the moment its displaying the textsearch and all classes regardless of what i select

    what am i doing wrong
  2. adam
    adam avatar
    15 posts
    Member since:
    Sep 2010

    Posted 21 Oct 2010 Link to this post

    i've got it working now.

    i do have another problem when i want to reset the radgrid back to show all in its default view. here is my full page code.

    the reset button either doesn't work or it does on the second click

    any ideas

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Search.ascx.cs" Inherits="Controls_Search" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <telerik:RadCodeBlock ID="code" runat="server">
    <script type="text/javascript">
     function comboLoad(sender, eventArgs) {
            sender.set_text(sender.get_items().getItem(0).get_value());
        }
     
        function StopPropagation(e) {
            if (!e) {
                e = window.event;
            }
     
            e.cancelBubble = true;
        }
     
        function nodeClicking(sender, args) {
            var comboBox = $find("<%= CategoryFilter.ClientID %>");
            var nodeValue = document.getElementById("<%= NodeValue.ClientID %>");
             
            var node = args.get_node()
            comboBox.set_text(node.get_text());       
            nodeValue.value = node.get_value();
     
            comboBox.trackChanges();
            comboBox.get_items().getItem(0).set_value(node.get_text());
            comboBox.commitChanges();
     
            comboBox.hideDropDown();
        }
     
        function FindNode() {
            var comboBox = $find("<%= CategoryFilter.ClientID %>");
            var treevw = comboBox.get_items().getItem(0).findControl("RadTree");
            var node = treevw.findNodeByValue(SelectedValue.value);
            node.select()
     
            comboBox.set_text(node.get_text());
     
            comboBox.trackChanges();
            comboBox.get_items().getItem(0).set_value(node.get_text());
            comboBox.commitChanges();
        }
       
    </script>
    </telerik:RadCodeBlock>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" >
        <AjaxSettings>              
            <telerik:AjaxSetting AjaxControlID="QuickSearchBox">
                <UpdatedControls>                   
                    <telerik:AjaxUpdatedControl ControlID="SearchGrid" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="SearchGrid">
                <UpdatedControls>                   
                    <telerik:AjaxUpdatedControl ControlID="SearchGrid" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting
            <telerik:AjaxSetting AjaxControlID="FilterBtn">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="SearchGrid" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="TypeFilter">
                <UpdatedControls>                   
                    <telerik:AjaxUpdatedControl ControlID="SearchGrid" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="CategoryFilter">
                <UpdatedControls>                   
                    <telerik:AjaxUpdatedControl ControlID="SearchGrid" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="ResetBtn">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="SearchGrid" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    <telerik:AjaxUpdatedControl ControlID="QuickSearchBox"></telerik:AjaxUpdatedControl>
                     <telerik:AjaxUpdatedControl ControlID="CategoryFilter"></telerik:AjaxUpdatedControl>
                      <telerik:AjaxUpdatedControl ControlID="TypeFilter"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>     
        </AjaxSettings>   
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" Transparency="20" BackColor="#ffffff" runat="server">
        <asp:Image ID="Image1" runat="server" AlternateText="Loading..." BorderWidth="0px" ImageUrl="~/Includes/images/ajax/Loading.gif" />
    </telerik:RadAjaxLoadingPanel>  
    <fieldset
        <asp:Label ID="SearchLabel" runat="server" AssociatedControlID="QuickSearchBox">Search Critical Care</asp:Label><br />
        <telerik:RadTextBox ID="QuickSearchBox"  runat="server"></telerik:RadTextBox><br />     
        <telerik:RadComboBox ID="TypeFilter" runat="server" OnSelectedIndexChanged="TypeFilter_SelectedIndexChanged" ></telerik:RadComboBox>
        <telerik:RadComboBox ID="CategoryFilter" runat="server"  OnClientLoad="FindNode" OnSelectedIndexChanged="CategoryFilter_SelectedIndexChanged">
        <Items>
            <telerik:RadComboBoxItem value="" Text="Filter by Category"/>
        </Items>
        <ItemTemplate>
            <div onclick="StopPropagation(event)">
                <telerik:RadTreeView ID="CategoryTreeView" runat="server" CheckBoxes="false"  DataFieldID="NodeID" DataFieldParentID="NodeParentID" DataTextField="CategoriesName" DataValueField="NodeID" OnClientNodeClicking="nodeClicking"></telerik:RadTreeView>
            </div>
        </ItemTemplate>
        <Items>
            <telerik:RadComboBoxItem Text=""/>
        </Items>
        </telerik:RadComboBox>
        <input id="NodeValue" value="" runat="server" enableviewstate="true" type="hidden" /><br />
        <asp:Button ID="FilterBtn" OnClick="Filter_Click" runat="server" Text="Filter" />
        <asp:Button ID="ResetBtn" OnClick="Reset_Click" runat="server" Text="Reset" />
        <telerik:RadFilter ID="SearchRadFilter" runat="server" Visible="false" ShowApplyButton="true" ExpressionPreviewPosition="None" ShowLineImages="true" FilterContainerID="SearchGrid"></telerik:RadFilter>
    </fieldset>
        <br />
        <br />
    <telerik:RadGrid ID="SearchGrid" runat="server" MasterTableView-ViewStateMode="Enabled" GroupingSettings-CaseSensitive="false" OnNeedDataSource="SearchGrid_NeedDataSource" OnItemDataBound="SearchGrid_ItemDataBound" OnItemCommand="SearchGrid_ItemCommand" AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true" PageSize="5">
        <GroupingSettings CaseSensitive="false"/>
        <MasterTableView AllowPaging="true" AllowSorting="true" AllowMultiColumnSorting="false">   
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Title" HeaderButtonType="TextButton" DataField="Title" SortExpression="Title" UniqueName="ArticleTitle" AutoPostBackOnFilter="true">
                <ItemTemplate>
                    <asp:Label ID="TitleLabel" Text='<%# Eval("Title") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Date" HeaderButtonType="TextButton" DataField="Date" SortExpression="Date" UniqueName="ArticleDate" AutoPostBackOnFilter="true">
                <ItemTemplate>
                    <asp:Label ID="DateLabel"  Text='<%# String.Format("{0:dd MM yyyy}",Eval("Date")) %>' runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Synopsis" HeaderButtonType="TextButton" DataField="Synopsis" SortExpression="Synopsis" UniqueName="ArticleSynopsis" AutoPostBackOnFilter="true">
                <ItemTemplate>
                    <asp:Label ID="SynopsisLabel"  Text='<%# Eval("Synopsis") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Author"  HeaderButtonType="TextButton" DataField="Author" SortExpression="Author" UniqueName="ArticleAuthor" AutoPostBackOnFilter="true">
                <ItemTemplate>
                    <asp:Label ID="AuthorLabel" Text='<%# Eval("Author") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
              <telerik:GridTemplateColumn HeaderText="Category" HeaderButtonType="TextButton" DataField="NodeParentID" SortExpression="NodeParentID" UniqueName="ArticleCategory" AutoPostBackOnFilter="true">
                <ItemTemplate>
                    <asp:Label ID="CategoryLabel" runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Link" HeaderButtonType="TextButton" DataField="Link" SortExpression="Link" UniqueName="ArticleTitle" AutoPostBackOnFilter="true">
                <ItemTemplate>
                    <asp:HyperLink ID="ArticleLink" NavigateUrl='<%# Functions.GetUrl(DataBinder.Eval(Container, "DataItem.NodeAliasPath")) %>' runat="server">Read</asp:HyperLink>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn  Visible="false" HeaderText="Text" HeaderButtonType="TextButton" DataField="Text" SortExpression="Text" UniqueName="ArticleText" AutoPostBackOnFilter="true">
               <ItemTemplate>
                    <asp:Label ID="TextLabel" Text='<%# Eval("Text") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>        
            <telerik:GridTemplateColumn Visible="false" HeaderText="ClassDisplayName" HeaderButtonType="TextButton" DataField="ClassDisplayName" SortExpression="ClassDisplayName" UniqueName="ArticleClassName" AutoPostBackOnFilter="true">
               <ItemTemplate>
                    <asp:Label ID="ClassDisplayName" Text='<%# Eval("ClassDisplayName") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>      
        </Columns>
        </MasterTableView>   
    </telerik:RadGrid>


    c#
    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    using CMS.CMSHelper;
    using CMS.GlobalHelper;
    using CMS.ISearchEngine;
    using Telerik.Web.UI;
     
     
    public partial class Controls_Search : System.Web.UI.UserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadItemGroups();
                LoadDocTypes();
            }
        }
     
        void LoadItemGroups()
        {
            DataSet ds = TreeHelper.SelectNodes("/%", false, "CriticalCare.Categories", null, "NodeOrder");
     
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                RadTreeView catagoryTreeview = (RadTreeView)this.CategoryFilter.Items[1].FindControl("CategoryTreeView");
                 
                int x = 0;
     
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    if (ds.Tables[0].Rows[x].ItemArray.GetValue(9).ToString() == "1")
                    {
                        row[9] = DBNull.Value;
                    }
                    x++;
                }
     
                catagoryTreeview.DataSource = ds;
                catagoryTreeview.MaxDataBindDepth = 2;
                catagoryTreeview.DataBind();
                catagoryTreeview.ExpandAllNodes();      
                 
            }
       }
     
        void LoadDocTypes()
        {
            DataSet ds = TreeHelper.SelectNodes("/%", false, "CriticalCare.Conclusion;CriticalCare.Literature;CriticalCare.Theory;CriticalCare.ClinicalApps;CriticalCare.CaseStudies;CriticalCare.Downloads;CriticalCare.Video", null, "ClassDisplayName Asc", -1, true);
     
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                DataTable dt = new DataTable();
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    dt.Merge(ds.Tables[i]);
                }
     
                IEnumerable<DataRow> types = (from row in dt.AsEnumerable()
                                                       group row by row.Field<string>("ClassDisplayName") into g
                                                       select g.FirstOrDefault());
     
                DataTable dtType = types.CopyToDataTable();
     
                TypeFilter.DataSource = dtType;
                TypeFilter.DataTextField = "ClassDisplayName";
                TypeFilter.DataValueField = "ClassDisplayName";
                TypeFilter.DataBind();
     
                TypeFilter.Items.Insert(0, new RadComboBoxItem("Filter by article type"));
            }
        }
     
        protected void SearchGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "FilterRadGrid")
            {
                SearchRadFilter.FireApplyCommand();
            }
        }
     
        protected void SearchGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataSet ds = TreeHelper.SelectNodes("/%", false, "CriticalCare.Conclusion;CriticalCare.Literature;CriticalCare.Theory;CriticalCare.ClinicalApps;CriticalCare.CaseStudies;CriticalCare.Downloads;CriticalCare.Video", null, "Date Asc", -1, true);
     
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                DataTable dt = new DataTable();
     
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    dt.Merge(ds.Tables[i]);
                }
     
                dt.DefaultView.Sort = "Date Desc";
                SearchGrid.DataSource = dt;
                
            }
            else
            {
                SearchGrid.Visible = false;
            }      
        }
     
        protected void SearchGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                DataRowView dr = (DataRowView)e.Item.DataItem;
     
                GridDataItem item = (GridDataItem)e.Item;
                Label Category = item.FindControl("CategoryLabel") as Label;
                int parentid = Convert.ToInt32(dr["NodeParentID"].ToString());
                int docid = TreeHelper.SelectSingleNode(parentid).DocumentID;
                CMS.TreeEngine.TreeNode node = TreeHelper.SelectSingleDocument(docid);
                string documentname = node.DocumentName.ToString();
                Category.Text = documentname;
            }
        }
     
        protected void Filter_Click(object sender, EventArgs e)
        {
            SearchRadFilter.RootGroup.Expressions.Clear();    
     
     
            if (QuickSearchBox.Text != "")
            {
                RadFilterGroupExpression textbox = new RadFilterGroupExpression();
                textbox.GroupOperation = RadFilterGroupOperation.Or;
                RadFilterContainsFilterExpression expression1 = new RadFilterContainsFilterExpression("Synopsis");
                RadFilterContainsFilterExpression expression2 = new RadFilterContainsFilterExpression("Title");
                RadFilterContainsFilterExpression expression3 = new RadFilterContainsFilterExpression("Text");
                expression1.Value = QuickSearchBox.Text;
                expression2.Value = QuickSearchBox.Text;
                expression3.Value = QuickSearchBox.Text;
                SearchRadFilter.RootGroup.AddExpression(textbox);
                textbox.AddExpression(expression1);
                textbox.AddExpression(expression2);
                textbox.AddExpression(expression3);
            }
     
            if (TypeFilter.SelectedValue != "")
            {
                RadFilterGroupExpression type = new RadFilterGroupExpression();
                RadFilterContainsFilterExpression expr = new RadFilterContainsFilterExpression("ClassDisplayName");
                expr.Value = TypeFilter.SelectedValue;
                SearchRadFilter.RootGroup.AddExpression(type);
                type.AddExpression(expr);
            }
     
            if (NodeValue.Value != "")
            {
                RadFilterGroupExpression category = new RadFilterGroupExpression();
                RadFilterContainsFilterExpression expr2 = new RadFilterContainsFilterExpression("NodeParentID");
                expr2.Value = NodeValue.Value;
                SearchRadFilter.RootGroup.AddExpression(category);
                category.AddExpression(expr2);
            }
             
            SearchRadFilter.FireApplyCommand();     
        }
     
        protected void TypeFilter_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            
            
            SearchRadFilter.FireApplyCommand();
        }
     
        protected void CategoryFilter_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
             
            SearchRadFilter.FireApplyCommand();
        }
     
        protected void Reset_Click(object sender, EventArgs e)
        {
            SearchRadFilter.RootGroup.Expressions.Clear();
            SearchGrid.Rebind();
            QuickSearchBox.Text = "";
            TypeFilter.SelectedIndex = 0;
            CategoryFilter.SelectedIndex = 0;
        }
    }


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 25 Oct 2010 Link to this post

    Hello Adam,

    You should clear RadGrid.MasterTableView.FilterExpressions before calling Rebind in order to bind RadGrid to the whole set of records.

    Greetings,
    Nikolay
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top