Using Hyperlink columns in GridView

Waqas Anwar
30 March 2008

GridView control is one of the most powerful controls in ASP.NET 2.0. This control displays database records in typical tabular form, and provides features such as Sorting, Paging, Selection and Editing without writing a single line of code. It also has many different types of fields (columns) such as hyperlinks, images, checkboxes etc.

In the following tutorial, I will show you different techniques you can use to display hyperlinks in GridView.  For this tutorial I am using Microsoft famous sample database Northwind and I am joining Products and Suppliers tables to display ProductID, ProductName, UnitPrice, SupplierID and CompanyName in the GridView. 


Databound hyperlink text with dynamic query string URL

      HeaderText="Product Name" 
      DataNavigateUrlFormatString="ProductDetails.aspx?ProductID={0}" />

Databound hyperlink text with multiple fields in the query string URL

      DataNavigateUrlFields="SupplierID, CompanyName" 
      SortExpression="CompanyName" />

Static hyperlink text with dynamic query string URL
      HeaderText="View Details" 
      Text="View Details" />

Static hyperlink text with external URL opening in new browser window

      HeaderText="Search Online" 
      Target="_blank" />

Databound hyperlink text with custom format to display link text
      DataTextFormatString="GBP {0}" 
      DataNavigateUrlFields="ProductID" HeaderText="Unit Price"
      DataNavigateUrlFormatString="ProductDetails.aspx?ProductID={0}"   />