1 out of 1 rated this helpful - Rate this topic

SPGridView Class

Represents a grid view that looks and behaves like a SharePoint Foundation list view.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class SPGridView : GridView, ICallbackEventHandler, 
	IPostBackEventHandler
Exception Condition
InvalidOperationException

AutoGenerateColumns is set to true.

The SPGridView control supports the cascading style sheets that are built intoMicrosoft SharePoint Foundation 2010. You can present data in a grid view that closely resembles the view that SharePoint Foundation 2010 uses to display data from lists. You can also sort and group rows.

This class does not support auto-generated columns. This means that you must explicitly bind each column using SPBoundField.

One way to populate the SPGridView control is to create an ADO.NET DataTable. You can then bind DefaultView to DataSource(), and then call DataBind(). You can do this by creating an ADO.NET DataTable or by using a query object, such as an SPSiteDataQuery object, that returns an ADO.NET DataTable. Additionally, you can use any ASP.NET data source control such as SPDataSource.

The following code shows how to set the values for this control.

<SharePoint:SPGridView runat="server"  ID="grdPropertyValues" AutoGenerateColumns="false" RowStyle-BackColor="#DDDDDD" AlternatingRowStyle-BackColor="#EEEEEE" />  
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
SPGridView.ClearFilter()
I had a a problem with this control which was I couldn't find a method to clear the applied filter after postback $0$0 $0 $0The solution is to create this method see the following link for more details http://mohamedramadanahmed.wordpress.com/2012/02/29/spgridview-clear-the-applied-fillter-programmatically-spgridview-clearfilter/
SPDataGrid error 'Unable to cast object of type int32 - SPDataGrid.NewPageIndex property and DataKey
When migrating from MOSS 2007 to SharePoint 2010 custom code call against SPGridView creates the following error when paging is used:
Unable to cast object of type 'System.Int32' to type 'System.String'

void spgvGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
     spgvGrid.PageIndex = e.NewPageIndex; //throws the error
     spgvGrid.DataBind();
}

I removed the error with removing the DataKeyNames="ID" from SPGridView. Because of that i had to get the ID of all checked checkboxs as suggested here:
http://blog.sharepointsite.co.uk/2010/10/spdatagrid-error-unable-to-cast-object.html

Is this a bug?

Regards,
Mark
Set style in the code behind.
If the grid view is created in the code behind file then this is how the style is set,

SPGrdiView = new SPGridView();
spGridView.AutoGenerateColumns = false;
spGridView.EnableTheming = true;
// RowStyle-BackColor="#DDDDDD" AlternatingRowStyle-BackColor="#EEEEEE"
spGridView.RowStyle.BackColor = System.Drawing.Color.Aqua;
spGridView.AlternatingRowStyle.BackColor = System.Drawing.Color.AntiqueWhite;
Using different DataSource Control as like Object DataSource
Hi,
Is it possible to bind data to the spgridview control by using SqlDatasource etc. as like object data source.