Geeks With Blogs

News

Steve Clements .Net and then some....

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 void AddColumn(DataControlField field)
  2. {
  3.     if (field == null) return;
  4.  
  5.     field.ItemStyle.CssClass = "ms-vb2";
  6.     field.HeaderStyle.CssClass = "ms-vh2-nofilter ms-vh2-gridview";
  7.     this.Columns.Add(field);
  8. }

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!!!

 

Posted on Monday, March 7, 2011 10:56 AM .NET 3.5 , Sharepoint | Back to top


Comments on this post: SharePoint 2010 Sandboxed solution SPGridView

# re: SharePoint 2010 Sandboxed solution SPGridView
Requesting Gravatar...
Steve, looks like the same code block added twice.
Left by Aaron Kowall on Mar 07, 2011 4:27 PM

# re: SharePoint 2010 Sandboxed solution SPGridView
Requesting Gravatar...
Thanks Aaron...doh...updated!!

It was late here :)
Left by Steve Clements on Mar 07, 2011 8:55 PM

# re: SharePoint 2010 Sandboxed solution SPGridView
Requesting Gravatar...
What about inheriting the theme if i am using Farm Solution? So i have got SPGrid Object and i just want it to be identicall to the rest of the branding.

Do i need to go through the same thing as above? or there is a single line method i can use ...

thanks
Left by On3 on Nov 22, 2011 6:50 PM

# re: SharePoint 2010 Sandboxed solution SPGridView
Requesting Gravatar...
It's basically just CSS, so you could override the SharePoint CSS class in your theme, then all grids will automatically take on that style.
Left by Steve Clements on Nov 22, 2011 11:59 PM

# re: SharePoint 2010 Sandboxed solution SPGridView
Requesting Gravatar...
No out of the box filtering capabilities going this route. This is just applying the CSS remember.
Left by yes but!!!!!! on Apr 09, 2012 7:26 PM

Your comment:
 (will show your gravatar)


Copyright © Steve Clements | Powered by: GeeksWithBlogs.net