Skip Ribbon Commands Skip to main content

SharePoint Happenings

Help (new window)
Sign In
Navigate Up
Get Microsoft Silverlight
Install Silverlight plugin for a richer experience...
Blog Home |  Freeware |  Speaking |  About me

Setting the CSS on a SPGridView Programmatically



Bookmark and Share

Top Tech Links










Top SharePoint Administrator Links









Top SharePoint Developer Links













NOTE: This blog has been moved to http://blog.philwicklund.com. If you'd like assistance, leave a comment on the copied post in the new location (hint, use search on the right to find the post).


Random topic – as you all know, it is well worth your time to use the SPGridView control because you can build really neat looking Data Grids from within SharePoint, that LOOK a lot like SharePoint – eg:

 

 

 

However, I had some specific requirements on colors/fonts and I needed to set a custom CSS class to my grid's CssClass property. I was having trouble getting this to work because it appeared the SPGridView kept over riding my changes within CreateChildControls. So, I overwrote OnPreRender and it worked! Here's my code:

 

public class SomeWebPart: System.Web.UI.WebControls.WebParts.WebPart

{

    SPGridView grid;

 

    protected override void OnPreRender(EventArgs e)

    {

        base.OnPreRender(e);

        grid.CssClass = "works";

    }

 

    protected override void CreateChildControls()

    {

        base.CreateChildControls();

 

        grid = new SPGridView();

 

        //grid.CssClass = "does't work";

//grid.Attributes["class"] = "doesn't work";

 

        // add columns, etc.

 

        this.Controls.Add(grid);

    }

}

 

 

Seems like the base class sets CSS and style propers in pre render – I guess that makes sense J

 

Phil