Custom Filter keeping previous search

5 posts, 0 answers
  1. Esteban
    Esteban avatar
    3 posts
    Member since:
    Sep 2013

    Posted 04 Sep 2013 Link to this post

    Hi,

    Im using a custom filter and want to use my own filter function (wanted to use wildcard options like * and ?), I followed the indications of this link: http://www.telerik.com/help/aspnet-ajax/grid-custom-option-for-filtering.html and the custom filter works.

    Nevertheless the grid keeps the previous filter values I used before, I mean, if I used the value "test*" for the name column it gets all the rows that the name starts with "test" but if I change the filter value and hit enter, the MasterTableView.FilterExpression value keeps the value of the filter I used before

    Here is my code

    aspx

    <asp:SqlDataSource ID="ds" SelectCommand="SELECT * FROM table" runat="server"></asp:SqlDataSource>
    <telerik:RadGrid ID="grd" runat="server" DataSourceID="ds" AutoGenerateDeleteColumn="false" AllowAutomaticDeletes="true" AllowFilteringByColumn="True" OnInit="RadGrid_Init" AllowMultiRowSelection="true" OnItemCreated="RadGrid_ItemCreated" OnItemCommand="RadGrid_ItemCommand">
     <ClientSettings>
        <Selecting AllowRowSelect="True"></Selecting>
        <ClientEvents OnRowSelected="Grid_RowSelected" OnRowDeselected="Grid_RowDeselected" />
    </ClientSettings>
    <MasterTableView ClientDataKeyNames="ID" AutoGenerateColumns="False">
    <Columns>
        <telerik:GridBoundColumn DataField="Name" HeaderText="Event Name" CurrentFilterFunction="Custom" AutoPostBackOnFilter="true" ShowFilterIcon="false"></telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Year" HeaderText="Event Year" CurrentFilterFunction="EqualTo" AutoPostBackOnFilter="true" ShowFilterIcon="false"></telerik:GridBoundColumn>
    </Columns>
    </MasterTableView>
    </telerik:RadGrid>

    c#
    protected void RadGrid_ItemCommand(object source, GridCommandEventArgs e)
       {
     
           if (e.CommandName == RadGrid.FilterCommandName)
           {
               Pair filterPair = (Pair)e.CommandArgument;
               if (filterPair.First.ToString() != "EqualTo")
               {
                   string colName = filterPair.Second.ToString();
                   TextBox tbPattern = (e.Item as GridFilteringItem)[colName].Controls[0] as TextBox;
                   string value = tbPattern.Text;
                   if (value != string.Empty)
                   {
                       e.Canceled = true;
                       string newFilter = string.Empty;
                       if (value.IndexOf("*") >= 0)  newFilter = "(([" + filterPair.Second + "] LIKE '" + value.Replace("*","%") + "'))";
                       else if (value.IndexOf("?") >= 0) newFilter = "(([" + filterPair.Second + "] LIKE '" + value.Replace("?", "%") + "'))";
                       else newFilter = "(([" + filterPair.Second + "] ='" + value + "'))";                
                       if (grd.MasterTableView.FilterExpression == "")
                       {
                           grd.MasterTableView.FilterExpression = newFilter;
                       }
                       else
                       {
                           grd.MasterTableView.FilterExpression = "((" + grd.MasterTableView.FilterExpression + ") AND (" + newFilter + "))";
                       }
                       grd.Rebind();
                   }
               }
           }
       }

    For instance, this is the value the grid is using after a couple of filter changes, the first value was 2010* and the second was *annu*.

     MasterTableView.FilterExpression = "(((([EventName] LIKE '2010%'))) AND ((([EventName] LIKE '%annu%'))))"

    I if clear the filter textbox, hit enter and then enter the new filter value it does clear the MasterTableView.FilterExpression value

    Thanks
  2. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 09 Sep 2013 Link to this post

    Hi Esteban,

    The experienced behavior is caused from the code in your else statement which appends the new filter expression to the current filter expression which keeps the old filters. In order to resolve your issue you use the code provided below which generates RadGrid FilterExpression string entirely and each expression could be changed manually. You could implement the same logic in the provided code snippet.
    string filterExpression = string.Empty;
    foreach (GridColumn column in this.RadGrid1.MasterTableView.RenderColumns)
    {
        if (!column.SupportsFiltering())
        {
            continue;
        }
        
        string filterText = column.EvaluateFilterExpression();
        // Modify the filterText value if the CurrentFilterValue contains *
        if (column.CurrentFilterValue.Contains('*'))
        {
                         
        }
                     
        if (String.IsNullOrEmpty(filterText))
        {
            column.ResetCurrentFilterValue();
            continue;
        }
     
        if (!String.IsNullOrEmpty(filterExpression))
        {
            filterExpression += " AND ";
        }
     
        filterExpression += "(" + filterText + ")";
    }
    this.RadGrid1.MasterTableView.FilterExpression = filterExpression;
    this.RadGrid1.Rebind();

    Regards,
    Antonio Stoilkov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Esteban
    Esteban avatar
    3 posts
    Member since:
    Sep 2013

    Posted 10 Sep 2013 Link to this post

    Hi Antonio,

    Thanks for your reply,

    I tried the option you put and it worked, but only gets the value of the first column filter entered. If I want to use two columns to filter the grid results, it ignores the second entry. 


    Esteban
  5. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 13 Sep 2013 Link to this post

    Hi Esteban,

    I have assembled a sample page showing the desired functionality fully implemented. You could take a look at the ItemCommand code below which successfully generates the RadGrid filter expression for multiple columns.
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.FilterCommandName)
        {
            e.Canceled = true;
            string filterExpression = string.Empty;
            foreach (GridColumn column in this.RadGrid1.MasterTableView.RenderColumns)
            {
                if (!column.SupportsFiltering())
                {
                    continue;
                }
     
                Pair filterPair = ((System.Web.UI.Pair)(e.CommandArgument));
                if (column.UniqueName == filterPair.Second.ToString())
                {
                    column.CurrentFilterFunction = (GridKnownFunction)Enum.Parse(typeof(GridKnownFunction), filterPair.First.ToString());
                }
     
                string filterText = column.EvaluateFilterExpression();
                // Modify the filterText value if the CurrentFilterValue contains *
                if (column.CurrentFilterValue.Contains('*') || column.CurrentFilterValue.Contains("?"))
                {
                    string value = column.CurrentFilterValue;
                    if (value.IndexOf("*") >= 0) filterText = "(([" + column.UniqueName + "] LIKE '" + value.Replace("*", "%") + "'))";
                    else if (value.IndexOf("?") >= 0) filterText = "(([" + column.UniqueName + "] LIKE '" + value.Replace("?", "%") + "'))";
                    else filterText = "(([" + column.UniqueName + "] ='" + value + "'))";               
                }
     
                if (String.IsNullOrEmpty(filterText))
                {
                    column.ResetCurrentFilterValue();
                    continue;
                }
     
                if (!String.IsNullOrEmpty(filterExpression))
                {
                    filterExpression += " AND ";
                }
     
                filterExpression += "(" + filterText + ")";
            }
            this.RadGrid1.MasterTableView.FilterExpression = filterExpression;
            this.RadGrid1.Rebind();
        }
    }

    Regards,
    Antonio Stoilkov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  6. Esteban
    Esteban avatar
    3 posts
    Member since:
    Sep 2013

    Posted 17 Oct 2013 Link to this post

    Thanks Antonio, this solution worked awesome!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017