Displaying Data by Using the SPGridView Control in Windows SharePoint Services 3.0

Applies to:  Microsoft Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

Ted Pattison, Critical Path Training

May 2007

When you create ASP.NET applications, you often need to display data in the format of rows and columns. Although you could create an HTML table by using code, you become much more productive by using server-side controls such as the GridView control introduced with Microsoft ASP.NET 2.0. The Microsoft Windows SharePoint Services platform provides a grid control named SPGridView that inherits from the ASP.NET GridView control. The SPGridView control provides a great alternative for displaying tabular data in SharePoint-based solutions.

When you add the SPGridView control to a custom application page, you must first add an @Register directive to the top of the page to reference the Microsoft.SharePoint assembly and to import the Microsoft.SharePoint.WebControls namespace.

After you add the @Register directive, you can create a control tag within an application page to instantiate the SPGridView control. Notice that the control tag must use the Tagprefix attribute value that is defined in the @Register directive.

<SharePoint:SPGridView 
   
  ID="grdPropertyValues" 
  AutoGenerateColumns="false"
  RowStyle-BackColor="#DDDDDD"
  AlternatingRowStyle-BackColor="#EEEEEE" />   
 

Unlike other grid controls used in ASP.NET, the SPGridView control does not support the automatic generation of columns. You receive a run-time error unless you assign the AutoGenerateColumns attribute a value of "false".

Populating the SPGridView Control with an ADO.NET DataTable

One easy way to populate the SPGridView control is to create an ADO.NET DataTable. That's because you can directly bind the DefaultView property of a DataTable to the DataSource property of the SPGridView control, and then call the DataBind method.

This example introduces a custom utility class named PropertyCollectionBinder, which encapsulates the work of creating and populating an ADO.NET DataTable object with two columns. It also provides a convenience method named BindGrid, which accepts a parameter reference to SPGridView control, and then populates it by data-binding the data within the ADO.NET DataTable.

Using the PropertyCollectionBinder Class to Bind Data

By using the PropertyCollectionBinder class in the code behind a custom application page, it becomes relatively simple to populate the SPGridView control with a set of name/value pairs. Here is an example of populating the SPGridView with some property values obtained by programming against the Windows SharePoint Services object model.

One advantage of using the SPGridView control in SharePoint-based solutions is that it has been integrated with the cascading style sheets that are built into Windows SharePoint Services 3.0. Also, the SPGridView control is heavily used in standard application pages and Web Parts that are built into Windows SharePoint Services. That means your custom application pages and Web Parts constructed using the SPGridView control can have the same look and feel as other aspects of the standard Windows SharePoint Services user interface.

Remember that the SPGridView control does not support the automatic generation of columns. That means you must explicitly bind each column using SPBoundField as you just saw. However, it is not difficult to supply the code you need to populate it with data. You can do this by creating an ADO.NET DataTable or by using a query object that return an ADO.NET DataTable object, such as an SPSiteDataQuery object.

Watch the Video

Length: 4:25 | Size: 15.3 MB | Type: WMV