SharePoint 2010 Sandboxed solution SPGridView

Posted by Steve Clements on Geeks with Blogs See other posts from Geeks with Blogs or by Steve Clements
Published on Mon, 07 Mar 2011 22:56:08 GMT Indexed on 2011/03/08 0:11 UTC
Read the original article Hit count: 531

Filed under:

If you didn’t know, you probably will soon, the SPGridView is not available in Sandboxed solutions.

To be honest there doesn’t seem to be a great deal of information out there about the whys and what nots, basically its not part of the Sandbox SharePoint API.

Of course the error message from SharePoint is about as useful as punch in the face…

An unexpected error has been encountered in this Web Part.  Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. You don't have Add and Customize Pages permissions required to perform this action

…that’s if you have debug=true set, if not the classic “This webpart cannot be added” !! Love that one!

but will a little digging you should find something like this…

[TypeLoadException: Could not load  type Microsoft.SharePoint.WebControls.SPGridView from assembly 'Microsoft.SharePoint, Version=14.900.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.]

 

Depending on what you want to do with the SPGridView, this may not help at all.  But I’m looking to inherit the theme of the site and style it accordingly.

After spending a bit of time with Chrome’s FireBug I was able to get the required CSS classes.  I created my own class inheriting from GridView (note the lack of a preceding SP!) and simply set the styles in there.

Inherit from the standard GridView

  1. public class PSGridView : GridView

 

Set the styles in the contructor…

  1. public PSGridView()
  2. {
  3.     this.CellPadding = 2;
  4.     this.CellSpacing = 0;
  5.     this.GridLines = GridLines.None;
  6.     this.CssClass = "ms-listviewtable";
  7.     this.Attributes.Add("style", "border-bottom-style: none; border-right-style: none; width: 100%; border-collapse: collapse; border-top-style: none; border-left-style: none;");
  8.  
  9.     this.HeaderStyle.CssClass = "ms-viewheadertr";
  10.     
  11.     this.RowStyle.CssClass = "ms-itmhover";
  12.     this.SelectedRowStyle.CssClass = "s4-itm-selected";
  13.     this.RowStyle.Height = new Unit(25);
  14. }

 

Then as you cant override the Columns property setter, a custom method to add the column and set the style…

  1. public PSGridView()
  2. {
  3.     this.CellPadding = 2;
  4.     this.CellSpacing = 0;
  5.     this.GridLines = GridLines.None;
  6.     this.CssClass = "ms-listviewtable";
  7.     this.Attributes.Add("style", "border-bottom-style: none; border-right-style: none; width: 100%; border-collapse: collapse; border-top-style: none; border-left-style: none;");
  8.  
  9.     this.HeaderStyle.CssClass = "ms-viewheadertr";
  10.     
  11.     this.RowStyle.CssClass = "ms-itmhover";
  12.     this.SelectedRowStyle.CssClass = "s4-itm-selected";
  13.     this.RowStyle.Height = new Unit(25);
  14. }

 

And that should be enough to get the nicely styled SPGridView without the need for the SPGridView, but seriously….get the SPGridView in the SandBox!!!

 

 

© Geeks with Blogs or respective owner