Export (0) Print
Expand All
Expand Minimize

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

Windows SharePoint Services 3

Visual How To

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

Ted Pattison, Critical Path Training

May 2007

Overview

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.

Code It

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.

<%@ Register 
  Tagprefix="SharePoint" 
  Namespace="Microsoft.SharePoint.WebControls" 
  Assembly="Microsoft.SharePoint, [4-part assembly name] " %> 

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 
  runat="server" 
  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 System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

public class PropertyCollectionBinder {
  protected DataTable PropertyCollection = new DataTable();
  public PropertyCollectionBinder() {
    PropertyCollection.Columns.Add("PropertyName", typeof(string));
    PropertyCollection.Columns.Add("PropertyValue", typeof(string));
  }
  public void AddProperty(string PropertyName, string PropertyValue) {
    DataRow newRow = PropertyCollection.Rows.Add();
    newRow["PropertyName"] = PropertyName;
    newRow["PropertyValue"] = PropertyValue;
  }
  public void BindGrid(SPGridView grid) {
    SPBoundField fldPropertyName = new SPBoundField();
    fldPropertyName.HeaderText = "Property Name";
    fldPropertyName.DataField = "PropertyName";
    grid.Columns.Add(fldPropertyName);
    SPBoundField fldPropertyValue = new SPBoundField();
    fldPropertyValue.HeaderText = "Value";
    fldPropertyValue.DataField = "PropertyValue";
    grid.Columns.Add(fldPropertyValue);
    grid.DataSource = PropertyCollection.DefaultView;
    grid.DataBind();
  }
}

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.

SPSite siteCollection = this.Site;
SPWeb site = this.Web;
PropertyCollectionBinder pcb = new PropertyCollectionBinder();
pcb.AddProperty("Site Title", site.Title);
pcb.AddProperty("Site ID", site.ID.ToString().ToUpper());
pcb.AddProperty("Current User Name", site.CurrentUser.Name);
pcb.BindGrid(grdPropertyValues);
Read It

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.

See It

Displaying Data in SPGridView Controls

Watch the Video

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

Explore It
Show:
© 2014 Microsoft