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

Best way to programatically alter filters

4 Answers 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John Hutchinson
Top achievements
Rank 1
John Hutchinson asked on 15 Mar 2012, 05:04 PM
Hi there,

I need to do some changing (not just creating an initial filter) of filters from 'outside' the radgrid (like adding a filter based on another dropdown). For example adding a filter to an already filtered grid from some event that occurs. I've seen things on using the filterExpression property, but I think that might get hairy if a filter is already applied and I want to manipulate it via string editing that property. Is there a better, more API-centric way to add/remove filters from external influences rather than hacking the filterExpression string?

Any pointers would be appreciated.

Thanks!

John

4 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 15 Mar 2012, 07:44 PM
John - I had a similar requirement - and "hacked" the filterexpression
the trick is to 1) turn off Linq
and 2) remove any existing filterexpression for the column

protected void cmbShipDate_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
    string filterExpression, oldFilter;
    WsOrderSystem wsOrder = new WsOrderSystem();
 
    oldFilter = rgEditOrder.MasterTableView.FilterExpression;
    filterExpression = wsOrder.RemoveFilter(oldFilter,"ShipDate");
    oldFilter = filterExpression;
    if (cmbShipDate.SelectedItem.Text == "All")
        filterExpression = string.Empty;
    else
        filterExpression = "([ShipDate] = '" + cmbShipDate.SelectedItem.Text + "')";
    rgEditOrder.MasterTableView.FilterExpression = wsOrder.AppendFilterExpression(oldFilter,filterExpression);
    rgEditOrder.EditIndexes.Clear();
    ClearOutItemData();
    rgEditOrder.MasterTableView.Rebind();
}

    public string RemoveFilter(string oldFilter,string columnName)
    {
        string filterExpression;
        string[] filterArray;
        string charac = "&";
        char[] amper = charac.ToCharArray();
        StringBuilder sb;
        int i;

        sb = new StringBuilder("");
   //   filterExpression = rgEditOrder.MasterTableView.FilterExpression;
        filterExpression = oldFilter.Replace(" AND ", charac);
        filterArray = filterExpression.Split(amper, 3);

        for (i = 0; i < filterArray.Length; i++)
        {
            if (filterArray[i].IndexOf(columnName) == -1)
            {
                if (sb.Length > 0)
                {
                    sb.Append(charac);
                }
                sb.Append(filterArray[i]);
            }
        }
        filterExpression = sb.ToString();
        filterExpression = filterExpression.Replace(charac, " AND ");
        return filterExpression;
    }


 
0
John Hutchinson
Top achievements
Rank 1
answered on 16 Mar 2012, 11:35 AM
Thanks Marianne!

Your solution works great. Just for completeness, can you post the code for wsOrder.AppendFilterExpression ? 

Now all I need is to trigger your add/remove from not only the dropdown changing (easy) but also from when the user clicks on the filter buttons of the radgrid itself. That way filters are maintained correctly from both places. Any thoughts?

Thanks,

John
0
Elliott
Top achievements
Rank 2
answered on 16 Mar 2012, 01:31 PM
gladly
oh and remove Linq - put EnableLinqExpressions=false on your grid

public string AppendFilterExpression(string oldFilter,string filterExpression)
 {
     StringBuilder sb;
//   sb = new StringBuilder(rgEditOrder.MasterTableView.FilterExpression);
     sb = new StringBuilder(oldFilter);
     if (filterExpression == string.Empty)
         return sb.ToString();
     if (sb.ToString() != string.Empty)
     {
         sb.Append(" AND ");
     }
     sb.Append(filterExpression);
     return sb.ToString();
 }


you could either make a common routine and invoke it out of your various event handlers or replicate the necessary functionality in however many click events you need to handle - your choice
0
Ricardo
Top achievements
Rank 1
answered on 18 Jul 2014, 10:12 PM
Could you post the ClearOutItemData() function, i am trying to replicate in my side. 
Tags
Grid
Asked by
John Hutchinson
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
John Hutchinson
Top achievements
Rank 1
Ricardo
Top achievements
Rank 1
Share this question
or