I've got a SPGridView in a webpart that has sorting, filtering, paging and grouping configured. Most of it works fine so far, but it seems like I can't get around this problem:

If I filter the content and sort it afterwards, the column I filtered with, seems to "forget" that it has been filtered. The content is still the filtered content, but you can't remove the filter any longer through the column as usual. If I sort first and then filter, it works.

I'm saving the filterExpression in the ViewState like so many tutorials explain, and set it back to the filterExpression of the DataTable the grid is bound to. This works for the content, but not for the column as it seems. I even found other posts of people who experienced this problem, but never with a solution for it.

Saving the filterExpression:

private void gridDS_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
        ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;

Setting the filterExpression of the DataTable when sorting the grid:

private void grid_Sorting(object sender, GridViewSortEventArgs e)
        if (ViewState["FilterExpression"] != null)
            gridDS.FilterExpression = (string)ViewState["FilterExpression"];

I also tried to do this in the PreRender Method or Page Load, I tried using a global variable instead of the viewstate, but nothing works. Everytime when I sort after i filtered, the column doesn't show anymore, that it has been filtered.

Is this a bug or did I probalbly do something wrong? Is there any way to tell the column it has been filtered manually?

2 Answers 2


SPGridView It's really painfull to work with. I have spent nearly 70 hours trying to do what are you saying.

Finally I could handle multiple SpGridViews with multifiltering saving the controls in an external class and working with them using a singleton pattern.

It's impossible to set multiple columns with the option "clear filter", you can just set one column.

The property to enable one column to being cleared is SPGridView.FilterFieldName. But you can't work with it since it can be just read.

Here is the trick: you can set SPGridView.FilterFieldName using SPGridView.Attributes["key"]


 SPGridView.Attributes["FilterFieldName"] = "yourcolumnname" 

will enable the action clear filter on that column and you will able to handle it in your LoadViewState event from your WebPart.


the solution provided by sandeces will only retain the field name where as there won't be a blue tick indicating that with which value it was filtered. To achieve that use the below.

SPGridView.Attributes["FilterFieldName"] = "yourcolumnname" 
SPGridView.Attributes["FilterFieldValue"] = "yourFilterValue" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.