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

Custom button to reset filter

5 Answers 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Softec
Top achievements
Rank 1
Softec asked on 25 Oct 2011, 08:08 AM
Hi

I would like to have a custom button in the column header to reset the current filter value. I did this by creating a custom command ResetFilter, with the UniqueName as CommandArgument and some logic on ItemCommand.

But I would like to use the regular grid mechanic, a button with the CommandName Filter and ComandArgument Pair("NoFilter", UniqueName). How is this achieved? I just end up with an exception of string could not be cast to System.Web.UI.Pair.

Thanks!

5 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 25 Oct 2011, 06:45 PM
could this snippet be what you are looking for?
rgEditOrder.MasterTableView.FilterExpression = string.Empty;
rgEditOrder.Rebind();
it is in the click event of a Clear All Filters button
0
Elliott
Top achievements
Rank 2
answered on 25 Oct 2011, 06:48 PM
to clear a single filter -

private string RemoveFilter(string columnName)
{
    string filterExpression;
    string[] filterArray;
    string charac = "&";
    char[] amper = charac.ToCharArray();
    StringBuilder sb;
    int i;
 
    sb = new StringBuilder("");
    filterExpression = rgEditOrder.MasterTableView.FilterExpression;
    filterExpression = filterExpression.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;
}

it returns the current filter with the filter for columnname cleared
0
Softec
Top achievements
Rank 1
answered on 26 Oct 2011, 01:06 PM
This is much more complicated than my solution. I don't like to fiddle with the filter expression by hand/stringbuilder.

What I did:

void TypedGrid_ItemCommand(object source, telerik.GridCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case (Grid.ResetFilterCommandName):
            {
                telerik.GridFilteringItem filterItem = e.Item as telerik.GridFilteringItem;
                if (filterItem != null)
                {
                    filterItem.FireCommandEvent(Grid.FilterCommandName, new ui.Pair(telerik.GridKnownFunction.NoFilter.ToString(), e.CommandArgument));
                }
            } break;
    }
}
But for this I have to know the Column UniqueName to set it as CommandArgument.
0
Elliott
Top achievements
Rank 2
answered on 26 Oct 2011, 09:43 PM
I just cut and paste a method which I used-
to build the filter this code worked:
filterExpression = "([ShipDate] = '" + cmbShipDate.SelectedItem.Text + "')";
so,do a text search for the first "([" then the UniqueName will be the string until the "]"
then look for the ")" then if there is an AND repeat
0
Elliott
Top achievements
Rank 2
answered on 27 Oct 2011, 02:34 PM
get the DataSetIndex property on the DataItem
then go to the MasterTableView of the grid and the column by that index-you'll need to know what kind of grid column it is
then get the UniqueName of that column
not sure if this works for templated columns
Tags
Grid
Asked by
Softec
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Softec
Top achievements
Rank 1
Share this question
or