Pagination, Sorting and Filtering in SPGridView

SPGridView is one of the most useful SharePoint controls. SPGridView is a grid view that looks and behaves like a SharePoint Foundation list view. The SPGridview allows us to present the data in a grid view that closely resembles the view that SharePoint Foundation 2010 uses to display data from lists.

Sorting in SPGridView

In this article I will explain how to do Pagination, Sorting and Filtering in SPGridView.

Use the following procedure to do it.

Step 1: Create a list with the following columns:
 
List

Step 2: Add a Visual WebPart to your project and drag and add a SPGridView into it.

Step 3: Add the following code on Page Load:
 

const string DATASOURCEID = "gridDS";

gridDS = new ObjectDataSource();

gridDS.ID = DATASOURCEID;

gridDS.SelectMethod = "SelectData";

gridDS.TypeName = this.GetType().AssemblyQualifiedName;

gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gvCRBooked_ObjectCreating);

this.Controls.Add(gridDS);

BindGrid();

Step 4: Add code for binding the SPGridView, enabling paging, sorting and Filtering.

private void BindGrid()

{

    gvCRBooked.AllowPaging = true

    // Sorting           

    gvCRBooked.AllowSorting = true

    //allow Filtering            

    gvCRBooked.FilterDataFields = "Title,ConferenceRoom,Location,FromDateTime,ToDateTime";

    gvCRBooked.FilteredDataSourcePropertyName = "FilterExpression";

    gvCRBooked.FilteredDataSourcePropertyFormat = "{1} = '{0}'";

    gvCRBooked.Sorting += new GridViewSortEventHandler(gvCRBooked_Sorting);

    gvCRBooked.PageIndexChanging += new GridViewPageEventHandler(gvCRBooked_PageIndexChanging);

    //For Filtering

    gridDS.Filtering += new ObjectDataSourceFilteringEventHandler(gvCRBooked_Filtering);

    gvCRBooked.AutoGenerateColumns = false;

    gvCRBooked.AllowFiltering = true;

    gvCRBooked.PagerTemplate = null;

    gvCRBooked.PageSize = 10;

    gvCRBooked.DataSourceID = gridDS.ID;

    gvCRBooked.DataBind();

}


Step 5: Code for Pagination in SPGridView:

void gvCRBooked_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

    gvCRBooked.PageIndex = e.NewPageIndex;

    gvCRBooked.DataSourceID = gridDS.ID;

    gvCRBooked.DataBind();

}

Step 6: Code for Sorting in SPGridView:

void gvCRBooked_Sorting(object sender, GridViewSortEventArgs e) 

{     
     string lastExpression = "";   

     if (ViewState["SortExpression"] != null

            lastExpression = ViewState["SortExpression"].ToString();

            string lastDirection = "asc";       

     if (ViewState["SortDirection"] != null)         

            lastDirection = ViewState["SortDirection"].ToString();

            string newDirection = string.Empty;          

    if (e.SortExpression == lastExpression)           

    {          

        e.SortDirection = (lastDirection == "asc") ? System.Web.UI.WebControls.SortDirection.Descending : System.Web.UI.WebControls.SortDirection.Ascending; 

    }       newDirection = (e.SortDirection == System.Web.UI.WebControls.SortDirection.Descending) ? "desc" : "asc";  

            ViewState["SortExpression"] = e.SortExpression;           

            ViewState["SortDirection"] = newDirection;

            gvCRBooked.DataBind();             //For Filter  

   if (ViewState["FilterExpression"] != null)           

   {             

      gridDS.FilterExpression = (string)ViewState["FilterExpression"]; 

   }    

}

Step 7: Code for Filtering in SPGridView:

private void gvCRBooked_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)

{         

    ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression; 

}  

Functionality

The code above explains how SPGridview actually works. The SPGridView looks up ObjectDataSource using the DataSourceID property. Once it has ObjectDataSource, it then sets the property specified in FilteredDataSourcePropertyName (FilterExpression) to be the filter. One more important point that to be kept in mind is that FilterDataFields should be in a sequence and if you don't want to add any filter to a specific column then enter a blank comma for this. For example:

"Title,,Location,FromDateTime,ToDateTime";

Conclusion

In this article we saw how to do sorting, filtering and paging in SPGridView.