Handle Multi-selection filtering in a custom column class

6 posts, 1 answers
  1. Kyle Smith
    Kyle Smith avatar
    42 posts
    Member since:
    Feb 2012

    Posted 27 Sep 2013 Link to this post

    I have a web application in which I generate Telerik RadGrids programatically based on some definition. Some of our datasets require custom template columns for which I have created special subclasses of GridTemplateColumn.

    Some of the functions I currently override to support single selection filtering:
    protected override void SetupFilterControls(TableCell cell)
     {
        base.SetupFilterControls(cell);
        cell.Controls.RemoveAt(0);
     
        var filterList = new RadComboBox();
        filterList.Width = FilterControlWidth;
        filterList.DropDownAutoWidth = RadComboBoxDropDownAutoWidth.Enabled;
        filterList.AutoPostBack = true;
        filterList.SelectedIndexChanged += filterList_SelectedIndexChanged;
        filterList.Items.Add(new RadComboBoxItem("", ""));
     
        // COMBO BOX IS POPULATED HERE -- CODE REMOVED
     
        cell.Controls.AddAt(0, filterList);
    }
     
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        base.SetCurrentFilterValueToControl(cell);
        if (CurrentFilterValue != "")
        {
            (cell.Controls[0] as RadComboBox).SelectedValue = CurrentFilterValue;
        }
    }
     
    protected override string GetCurrentFilterValueFromControl(TableCell cell)
    {
        return (cell.Controls[0] as RadComboBox).SelectedValue;
    }
     
    private void filterList_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)sender).NamingContainer;
     
        filterItem.FireCommandEvent("Filter", new Pair("EqualTo", UniqueName));
    }

    Everything here works as I expected, I am able to filter the grids by selecting an item in the radcombo box.

    My question is, how can I modify this code to allow multiple selections (via the checkbox feature of the radcombo) and have my grid filter to contain the rows with any of the selected values?

    The issue would seem to be with the filterItem.FireCommmandEvent. There doesn't seem to be a way to indicate an "IN" filter or ".Contains" in LINQ syntax. Is what I am trying to do not possible? I've see the example solution referenced in other threads, but that involves editing the filter expressions on the grid directly. Is that something I can do from the column itself?
  2. Answer
    Eyup
    Admin
    Eyup avatar
    3012 posts

    Posted 02 Oct 2013 Link to this post

    Hello Kyle,

    Generally, this requirement will be in-built in the upcoming release:
    http://demos.telerik.com/aspnet-ajax-beta/grid/examples/generalfeatures/filtering/defaultcs.aspx

    Alternatively, you can manipulate the filter expression:
    http://www.telerik.com/community/code-library/aspnet-ajax/grid/multi-selection-radcombobox-for-filtering-grid.aspx#2351833

    The third option would be to extend the custom column implementation:
    Copy Code
    public override string EvaluateFilterExpression(GridFilteringItem filteringItem)
    {
        if (!this.SupportsFiltering())
        {
            return string.Empty;
        }
     
        TableCell cell = filteringItem[this.UniqueName];
        string filterValue = this.GetCurrentFilterValueFromControl(cell);
        if (String.IsNullOrEmpty(filterValue) && !FunctionTakesNoArguments(this.CurrentFilterFunction))
        {
            return "";
        }
        else
        {
            GridFilterFunction filterFunc = new GridFilterFunction(this.CurrentFilterFunction);
            //Get DataType of colum
            string[] values = filterValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
     
            List<string> expressions = new List<string>();
            string expression = string.Empty;
     
            foreach (string value in values)
            {
                expressions.Add("(it[\"" + this.UniqueName + "\"].ToString().Contains(\"" + value + "\"))");
            }
            return string.Join("OR", expressions);
        }
    }
    private bool FunctionTakesNoArguments(GridKnownFunction filterFunction)
    {
        return filterFunction == GridKnownFunction.IsEmpty
                        || filterFunction == GridKnownFunction.NotIsEmpty
                        || filterFunction == GridKnownFunction.IsNull
                        || filterFunction == GridKnownFunction.NotIsNull;
    }

    Hope this helps.

    Regards,
    Eyup
    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. Kyle Smith
    Kyle Smith avatar
    42 posts
    Member since:
    Feb 2012

    Posted 02 Oct 2013 Link to this post

    Excellent! Thank you for the suggested solutions, I will give those a try.

    Looking forward to the upcoming release!
  5. Brandon Sheffer
    Brandon Sheffer avatar
    1 posts
    Member since:
    Nov 2009

    Posted 23 Feb 2015 in reply to Eyup Link to this post

    Hi Eyup,

    You mention that this will be available as a standard feature. I'm having some trouble finding anything stating that. Has this officially made it into a release or should we still try to override the filtering function?

    Thanks!!
  6. Eyup
    Admin
    Eyup avatar
    3012 posts

    Posted 26 Feb 2015 Link to this post

    Hello Brandon,

    You can check the built-in functionality in the second grid of the following live sample:
    http://demos.telerik.com/aspnet-ajax/grid/examples/functionality/filtering/basic-filtering/defaultcs.aspx

    I hope this will prove helpful.

    Regards,
    Eyup
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Eyup
    Admin
    Eyup avatar
    3012 posts

    Posted 10 Sep 2015 Link to this post

    Hi,

    I've uploaded several RadGrid web site samples in the following forum post to demonstrate specific scenarios with customized filter templates:
    http://www.telerik.com/forums/how-to-persist-dropdownlist-selected-index-and-value-on-postback-from-radgrid-filtertemplate#GW3MyQLmVEmy8XzsmrHzeQ

    I hope they prove helpful.

    Regards,
    Eyup
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017