Override filter expression

3 posts, 0 answers
  1. Tristan
    Tristan avatar
    2 posts
    Member since:
    Feb 2012

    Posted 24 Jun 2013 Link to this post

    I am using a filter control in conjunction with a grid. The columns available in the grid change depending on user preferences. When a string column is selected in the filter control; two of the options available are 'Value Exists' and 'No Value Exists'. The expressions generated by these options are [ColumnName] != null and [ColumnName] == null. Is it possible to override this behavior such that an empty string would NOT be considered a value.

    ie.
    Value Exists => Any value that is not null or empty
    No Value Exists => Any null or empty value.

  2. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 26 Jun 2013 Link to this post

    Hi Tristan,

    You could achieve your scenario by manually adding the RadFilterIsEmptyFilterExpression and RadFilterNotIsEmptyFilterExpression expressions in the RadFilter ApplyExpressions event handler. Note that this approach will require to manually filter the RadGrid as shown in the help article below.
    protected void RadFilter1_ApplyExpressions(object sender, Telerik.Web.UI.RadFilterApplyExpressionsEventArgs e)
    {
        RadFilterGroupExpression rootGroupClone = new RadFilterGroupExpression();
        for (int i = 0; i < RadFilter1.RootGroup.Expressions.Count; i++)
        {
            RadFilterIsNullFilterExpression nullExpression = RadFilter1.RootGroup.Expressions[i] as RadFilterIsNullFilterExpression;
            RadFilterNotIsNullFilterExpression notIsNullExpression = RadFilter1.RootGroup.Expressions[i] as RadFilterNotIsNullFilterExpression;
            if (nullExpression != null)
            {
                RadFilterIsEmptyFilterExpression emptyExpression = new RadFilterIsEmptyFilterExpression(nullExpression.FieldName);
                rootGroupClone.AddExpression(emptyExpression);
            }
            else if (notIsNullExpression != null)
            {
                RadFilterNotIsEmptyFilterExpression notIsEmptyExpression = new RadFilterNotIsEmptyFilterExpression(notIsNullExpression.FieldName);
                rootGroupClone.AddExpression(notIsEmptyExpression);
            }
            rootGroupClone.AddExpression(RadFilter1.RootGroup.Expressions[i]);
        }
     
        RadFilterSqlQueryProvider queryProvider = new RadFilterSqlQueryProvider();
        queryProvider.ProcessGroup(e.ExpressionRoot);
        this.RadGrid1.MasterTableView.FilterExpression = queryProvider.Result;
        this.RadGrid1.MasterTableView.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. Tristan
    Tristan avatar
    2 posts
    Member since:
    Feb 2012

    Posted 02 Jul 2013 Link to this post

    Hi Antonio,

    Thank you for your assistance. I had to make a couple of changes to get your code to work but it pointed me in the right direction. Code provided below in case this helps anyone else.

    protected void ftrFilter_ApplyExpressions(object sender, RadFilterApplyExpressionsEventArgs e)
    {
        var rootGroupClone = new RadFilterGroupExpression();
        for (int i = 0; i < ftrFilter.RootGroup.Expressions.Count; i++)
        {
            var nullExpression = ftrFilter.RootGroup.Expressions[i] as RadFilterIsNullFilterExpression;
            var notIsNullExpression = ftrFilter.RootGroup.Expressions[i] as RadFilterNotIsNullFilterExpression;
            if (nullExpression != null)
            {
                var group = new RadFilterGroupExpression();
                group.GroupOperation = RadFilterGroupOperation.Or;
     
                var emptyExpression = new RadFilterIsEmptyFilterExpression(nullExpression.FieldName);
                group.Expressions.Add(emptyExpression);
                group.Expressions.Add(nullExpression);
                rootGroupClone.AddExpression(group);
            }
            else if (notIsNullExpression != null)
            {
                var group = new RadFilterGroupExpression();
                group.GroupOperation = RadFilterGroupOperation.Or;
                var notIsEmptyExpression = new RadFilterNotIsEmptyFilterExpression(notIsNullExpression.FieldName);
                group.Expressions.Add(notIsEmptyExpression);
                group.Expressions.Add(notIsNullExpression);
                rootGroupClone.AddExpression(group);
            }
            else
                rootGroupClone.AddExpression(ftrFilter.RootGroup.Expressions[i]);
        }
     
                 
        var queryProvider = new RadFilterDynamicLinqQueryProvider();
        queryProvider.ProcessGroup(rootGroupClone);
        grdSearchResults.MasterTableView.FilterExpression = queryProvider.Result;
        grdSearchResults.MasterTableView.Rebind();
    }
Back to Top