HyperLinkField in SPGridView using Moss 2007 Object model


This article is going to explain

  1. How to work with SPGridView
  2. How to add HypeLinkField on a SPGridView.
  3. How to Put SPGridView on a web part.
  4. How to deploy the web part on a sharepoint site.


I have a SharePoint list called TestingHyperLink. Columns of SharePoint list are as below.

  1. Name column is of type Single Line of Text.
  2. Favorite WebSite column is of type HyperLink or Picture
  3. Favorite Site column is of type HyperLink or Picture



When opening task T1

I have created a SharePoint view for the List. View contains only columns Name,Favorite WebSite and Favorite List. I have given the name of the view as MyView.

Displaying in SPGridView in Web Part

Note : I am using Visual Studio Extension 2008 for WSS 3.0 . If you don’t have installed on your visual studio download and install that.

  1. Create a New Web Part by selecting File –> New -> SharePoint -> Web Part
  2. Add Reference of System.Data
  3. Write coding to fetch data


using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using System.Data;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace WebPart1



WebPart1 : System.Web.UI.WebControls.WebParts.WebPart


SPSite mySite;

SPWeb myWeb;

SPList myList;

SPGridView myView;

public WebPart1()



void CreateChildControls()



SPSite mySite = new

SPWeb myWeb = mySite.OpenWeb();

myList = myWeb.Lists[“TestingHyperLink”];

myView = new

SPGridView sp = BindToGrid(myList, myView );

Panel panel1= new
Panel() ;



SPGridView BindToGrid(SPList myList,SPGridView myView)


//myView = new SPGridView();

SPView sharepointview = myList.Views[“MyView”];

SPListItemCollection listCollection = myList.GetItems(sharepointview);

DataTable dt = new

dt.Columns.Add(“Name”, typeof(string));



DataRow dtRow;

foreach (SPListItem listitem in listCollection)


dtRow = dt.Rows.Add();

dtRow[“Name”] = listitem[“Name”];

string favwebsite = (string)listitem[“Favorite WebSite”];

string favwebsiteUri = GetUri(favwebsite);

dtRow[“FavWebSite”] = favwebsiteUri;

string favlist = (string)listitem[“Favorite List”];

string favlistUri = GetUri(favlist);

dtRow[“FavList”] = favlistUri;


SPBoundField boundfield = new

boundfield.HeaderText = “Name”;

boundfield.DataField = “Name”;


HyperLinkField hyperFieldfavsite = new

string[] favsitearray = new

favsitearray[0] = “FavWebSite”;

hyperFieldfavsite.DataTextField = “FavWebSite”;

hyperFieldfavsite.DataNavigateUrlFields = favsitearray;

hyperFieldfavsite.DataNavigateUrlFormatString = “{0}”;


HyperLinkField hyperFieldfavlist = new

string[] favlistarray = new

favsitearray[0] = “FavList”;

hyperFieldfavsite.DataTextField = “FavList”;

hyperFieldfavsite.DataNavigateUrlFields = favlistarray;

hyperFieldfavsite.DataNavigateUrlFormatString = “{0}”;


myView.AutoGenerateColumns = false;

myView.DataSource = dt.DefaultView;


// Panel1.Controls.Add(myView);

return myView;


string GetUri(string str)


string[] stemp = str.Split(“,”.ToCharArray());

string s1 = stemp[0];

string s2 = stemp[1];

HyperLink h = new

h.NavigateUrl = s1;

return h.NavigateUrl;





  1. TestingHyperLink is name of the list.
  2. MyView is name of the view.
  3. BindToGrid method is just creating Data table. Adding the column to data table and populating the row.
  4. HypeLinkField is used to populate the column as link.
  5. GetUri method is constructing URI from the string.
  6. Complie the solution.
  7. To deploy put the link of the site collection. Where you want to deploy the web part. Right click on Solution then properties then click on Debug tab. In Start Browser field put URL of site collection.


  8. Recompile the code.
  9. Right click and Package the solution

  10. Deploy the solution. Again right click on solution and click Deploy.

  1. Do an IISRESET. Go to command prompt and type this IISRESET. This is to restart the IIS.
  2. Open the SharePoint site.
  3. Go to Site Action then Site Setting
  4. Go to Site Collection Feature under Site Collection Administration tab. You should able to see the web part with SPGridView you deployed now.

  5. Add web part from Edit mode to view the Grid View.


I have shown in this article, how to add HyperLink field in SPGridView and then deply SPGridView on a webpart. Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com