.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

HyperLinkField in SPGridView using Moss 2007 Object model

Posted By:Dhananjay Kumar       Posted Date: December 24, 2009    Points: 25    Category: SharePoint    URL: http://www.dotnetspark.com  

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
    public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
        SPSite mySite;
        SPWeb myWeb;
        SPList myList;
        SPGridView myView;
        public WebPart1()

        protected override void CreateChildControls()

            SPSite mySite = new SPSite("http://adfsaccount:2222/");
            SPWeb myWeb = mySite.OpenWeb();
            myList = myWeb.Lists["TestingHyperLink"];
            myView = new SPGridView();

            SPGridView sp = BindToGrid(myList, myView );
            Panel panel1= new Panel() ;

        private SPGridView  BindToGrid(SPList myList,SPGridView myView)
            //myView = new SPGridView();
            SPView sharepointview = myList.Views["MyView"];
            SPListItemCollection listCollection = myList.GetItems(sharepointview);
            DataTable dt = new DataTable();

            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 SPBoundField();
            boundfield.HeaderText = "Name";
            boundfield.DataField = "Name";
            HyperLinkField hyperFieldfavsite = new HyperLinkField();
            string[] favsitearray = new string[1];
            favsitearray[0] = "FavWebSite";
            hyperFieldfavsite.DataTextField = "FavWebSite";
            hyperFieldfavsite.DataNavigateUrlFields = favsitearray;
            hyperFieldfavsite.DataNavigateUrlFormatString = "{0}";
            HyperLinkField hyperFieldfavlist = new HyperLinkField();
            string[] favlistarray = new string[1];
            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;

        private string GetUri(string str)
            string[] stemp = str.Split(",".ToCharArray());
            string s1 = stemp[0];
            string s2 = stemp[1];
            HyperLink h = new HyperLink();
            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. 

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

  15. 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. 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend