Pages

Showing posts with label SPGridView. Show all posts
Showing posts with label SPGridView. Show all posts

Wednesday, March 14, 2012

Apply CSS for SPGridView Programatically in Webpart

Hello Friends

I have been working on webpart in which I was using SPGridView. Now I want to apply  fore-color to one column inside the SPGridView. When I tried with everything and none of them was working , every time it gets color from CSS file then I tried below line it works like magic!!!

oGridColCourse.ItemStyle.CssClass = "color: #0000F5 !important;";

Happy Coding!!!
Disha Shah

Wednesday, February 15, 2012

Rendering HTML and Multiple HyperLink inside SPGridView

Sometimes we need to include HTML formatted data within some fields of SPGridView. I have worked on one requirement in which I have to include multiple Hyperlinks into one field of HTML in which I have written like “<a>” , and when SPGridView renders it shows entire HTML Code, not hyperlinks.

An easy way around this is to set the HTMLEncode property to false. By default this property is set to true, which will encode your HTML so that it will show up as code, changing <’s and >’s into &lt; and &gt;, and so on.

Sample Code:

#region Create Child Controls
protected override void CreateChildControls()
{
    try
    {

        grdData = new SPGridView();
        grdData.AutoGenerateColumns = false;

        BoundField colTitle = new BoundField();
        colTitle.HeaderText = "Title";
        colTitle.HtmlEncode = false; //This is the line
        colTitle.DataField = "Title";
        this.grdData.Columns.Add(colTitle);

        …

        this.Controls.Add(this.grdData);

    }
    catch (Exception ex)
    {
    }
}
#endregion


Enjoy!!!