This is a migrated thread and some comments may be shown as answers.

Handle Multi-selection filtering in a custom column class

5 Answers 463 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyle Smith
Top achievements
Rank 1
Kyle Smith asked on 27 Sep 2013, 06:14 PM
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?

5 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 02 Oct 2013, 08:13 AM
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.
0
Kyle Smith
Top achievements
Rank 1
answered on 02 Oct 2013, 01:53 PM
Excellent! Thank you for the suggested solutions, I will give those a try.

Looking forward to the upcoming release!
0
Brandon Sheffer
Top achievements
Rank 1
answered on 23 Feb 2015, 02:12 PM
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!!
0
Eyup
Telerik team
answered on 26 Feb 2015, 10:17 AM
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.

 
0
Eyup
Telerik team
answered on 10 Sep 2015, 08:27 AM
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
Tags
Grid
Asked by
Kyle Smith
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Kyle Smith
Top achievements
Rank 1
Brandon Sheffer
Top achievements
Rank 1
Share this question
or