Using SPGridView with Microsoft Office SharePoint Server 2007

Some developers new to SharePoint might not understand why they would want to use a SPGridView control over a ASP.NET GridView control for rendering lists of data.   I think the #1 reason for me is  that the SPGridView control makes your custom web parts look just like the out of the box SharePoint list views. It also has some very nice sorting and filtering functionality built in when used with an ObjectDataSource.

In a previous post I explained how you could create an ObjectDataSource from a DataTable.  In this post I will show how to utilize the ObjectDataSource with the SPGridView to allow for easy sorting and filtering of data.

To get started we need to implement the DataTableWrapper class shown below.    We will also need to define an instance of the DataTableWrapper class and a DataTable object that will be used for our actual source of data for the grid.

public class DataTableWrapper
{
    private DataTable _dt = new DataTable();
    public DataTableWrapper(DataTable dt)
    {
            _dt = dt;
     }

 
     public DataTable GetTable()
     {
             return _dt;
      }

 }

 
    DataTable sourceDataTable;
    DataTableWrapper myDataTable;

Now that we have our supporting wrapper class and a couple of objects defined it is now time to assign an actual DataTable instance to the sourceDataTable object.   Do this using whatever method is appropriate for your situation.   The goal is to have a DataTable object called sourceDataTable filled with the records you wish to display within the SPGridView control.

The code below creates an instance of the DataTableWrapper class based upon the sourceDataTable. This is done so we can retrieve the qualified type name of our DataTableWrapper object which is required by the ObjectDataSource.

An ObjectDataSource is then created which will be used as the actual source of data for the SPGridView.   It is very important to make sure you add the ObjectDataSource to your controls collection.  If you skip that step you will get a fairly cryptic error message which really doesn’t help identify the root cause of the issue.

 //Create instance of wrapper to grab type.
            myDataTable = new DataTableWrapper(sourceDataTable);
            Type t = myDataTable.GetType();

 
 //Create ObjectDataSource
            ObjectDataSource ds = new ObjectDataSource();
            ds.ID = "myDataSource";
            ds.TypeName = t.AssemblyQualifiedName;
            ds.SelectMethod = "GetTable";
            ds.ObjectCreating += new ObjectDataSourceObjectEventHandler(ds_ObjectCreating);
            this.Controls.Add(ds);

 

To use a custom object (such as our DataTableWrapper) with the ObjectDataSource you need to handle the ObjectCreating event as shown below.   This creates an instance of the DataTableWrapper based on our sourceDataTable.   The ObjectDataSource will then call the GetTable method from our wrapper class whenever it needs to get all of the records from our sourceDataTable.

void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
            myDataTable = new DataTableWrapper(sourceDataTable);
            e.ObjectInstance = myDataTable;
}

We are now ready to create our SPGridView control. One thing to know about the SPGridView is that you must set AutoGenerateColumns property to false and then manually create the columns.  If you do not do this you will receive an error.   If you wish to enable sorting for columns then set the AllowSorting property to true.

 //Create Grid
            SPGridView fileListingGrid = new SPGridView();
            fileListingGrid.AutoGenerateColumns = false;
            fileListingGrid.AllowSorting = true

Add a BoundField object as a column to the SPGridView for each column you wish to display.  If you want to control the formatting of the data or add controls to the SPGridView you will need to create template columns.  Using template columns with the SPGridView is the same as using them with the ASP.NET GridView control.   For an example see:  Dynamic Template Columns in the ASP.NET 2.0 GridView Control.

BoundField myField = new BoundField();
myField.HeaderText = "The Title";
myField.DataField = "Title";
myField.SortExpression = "Title";
fileListingGrid.Columns.Add(myField);

I think the properties are self-explanatory for the BoundField object so I won’t go into too much detail here.  I do want to point out that in order for automatic sorting you must set the SortExpression property.   This should, in most instances, be the same value as the DataField property.

Once all of the columns have been defined for the SPGridView control you can set the DataSourceID property to the ID of the ObjectDataSource.   In our case we set the SPGridView’s DataSourceID property to myDataSource.

You will notice that prior to calling the DataBind method on our SPGridView object we are adding it to our controls collection.  If you try to call the DataBind method prior to adding the control to the controls collection you will receive an error.

fileListingGrid.DataSourceID = "myDataSource";
this.Controls.Add(fileListingGrid);
fileListingGrid.DataBind();

You should now have a working SPGridView control that implements basic sorting.

To enable filtering you need to set a few other properties on the SPGridView control prior to calling the DataBind method.  Below you will find an example of the properties that need to be set in order to enable filtering.   I recommend the blog post Filtering with SPGridView for in depth details on how to configure filtering.

fileListingGrid.AllowFiltering = true;
fileListingGrid.FilterDataFields = "Title";
fileListingGrid.FilteredDataSourcePropertyName = "FilterExpression";
fileListingGrid.FilteredDataSourcePropertyFormat = "{1} like '{0}'";

As you can see using an ObjectDataSource with a SPGridView control allows you to get sorting, filtering with a consistent SharePoint look and feel with very little effort.

0 thoughts on “Using SPGridView with Microsoft Office SharePoint Server 2007”

Leave a Reply