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

Setting RadGrid FilterExpression programmatically

1 Answer 882 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Trevor
Top achievements
Rank 1
Trevor asked on 18 Feb 2011, 11:51 PM
I have a RadGrid who's filter expression I am setting from a saved value in a datastore on page load.  Setting the filter programmatically seems to work just fine, however, as soon as I try to modify the filter (manually through the web interface) I loose all the filter information set in the page load method.  This only happens the first time I change the filters after loading the page.  After that, all filtering is persisted properly.  Here's the code that programmatically sets the filter expresssion:


protected void LoadFilterExpressionFromSettings(GridTableView tbView, string filterExpression, Dictionary<string, Pair> dicColumns)
{
    tbView.FilterExpression = Settings.ListFilterExpression;
    foreach(KeyValuePair<string, Pair> p in dicColumns)
    {
        tbView.GetColumn(p.Key).CurrentFilterValue = ((Pair)p.Value).First.ToString();
        tbView.GetColumn(p.Key).CurrentFilterFunction = (GridKnownFunction)((Pair)p.Value).Second;
    }
}



The FilterExpression is set on the first line.  The loop goes through each column to set the GirdFilterItem's TextBox.

------------------------------------------------------------------------------------------------------------------------------

Example (of what's going one):

RadGrid:

FirstName               LastName             Age
=================================
Bob                         Johnson                 28
Jane                        Johnson                 28
Sue                         Hanson                  28
John                        Smith                     31
Henry                      Smith                     23
Hilary                      Porter                     31 
=================================

On page load, I might set a filter for the 'LastName' column ('Johnson').  After the page loads, I try to manually filter the age column to '28' but it resets the 'LastName' column filter (to NoFilter).  When I change the 'LastName' column filter back to 'Johnson', it works just fine and the age filter persists as well.  So it's just the first change I make that resets the filters which were programmatically set.

Any help is very welcome!

Trevor



1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 24 Feb 2011, 09:01 AM
Hi Trevor,

The default behavior for the grid is not to persist the current filter function on postback if you do not have value entered in the filter text box and have not filtered by it.
You can try setting the current filter function in the Page_PreRender event this way it will be persisted. If the data needs to be filtered you also need to rebind the grid there.
Please refer to the following help article for more information on setting filter expressions programmatically on PreRender.

Greetings,
Marin
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
Grid
Asked by
Trevor
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or