SPGridView : Filtering

I wanted to use a little SPGridView in a software and had a little problem. First of all, in order to use filtering on a SPGridView, you have to give an ObjectDataSource by it’s control’s ID. For anything else, you can use the DataSource property directly.

The best sample code I could fin on the net was this one : Creating the SPGridView with sorting and adding filtering.

This example is great because it only shows the basic requirements to setup a SPGridView and it gives you a clean way to build the ObjectDataSource and it explains step by step why you have to do things this way (in Sharepoint, it’s very important).

The problem, and this is the subject of this post, came when I activated the filtering functionnality. The menu displayed and then got stuck on “Loading…” with an alert message saying it got a NullReferenceException. Here is the pic :

I finally found the origin of my problem. In my first version, I was giving the DataSource to my SPGridView on the “PreRender” loading step. For the AJAX call, you never reach this step, you have to give the DataSource sooner.

Once I got that right, I just cached the last DataSource in the ViewState to give in the CreateChildControls method and it worked. The menu was finally displaying fine.

It can’t exactly give you my code (because it’s not legally mine), but here is the basic Idea :

In the real world usage, you will give your DataTable to a property so that it can be displayed later by your SPGridView. So you can basically just copy/paste the ASPGridView class given by Erik Burger and just replace the “SelectData” method by this one :

public DataTable SelectData {
    return DataTable;
public DataTable DataTable {
    get {
        return ViewState["DataTable"] as DataTable;
    set {
        ViewState["DataTable"] = value;

And anywhere in your WebPart’s OnPreRender method, or in any button’s event receiver method, you could build your DataSource.

3 thoughts on “SPGridView : Filtering”

  1. Its a great post,however i’m using SPgrid view and trying to access and display lists from other subsites as well,for that i’m creating object of the site and from there opening web, getting list and list items.So,i can’t understand where to put this so as to get the working model ready for use.I’m getting the same error as u’ve mentioned but can’t find out the solution for this.Any help would be highly appreciated..

  2. It’s been a long time since I wrote this and I don’t exactly remember everything.

    I’m not sure to understand what you say. But if it’s the same error that I had, you have to make sure that your data is loaded when the AJAX call is made. The easiest way to do that is to override some of the methods of the ASP.Net page lifecycle (http://msdn.microsoft.com/en-us/library/ms178472.aspx), put some breakpoints in them and make sure that your code is executed during the AJAX call, and if not check where you could put it.

Leave a Reply

Your email address will not be published. Required fields are marked *