In this article I will explain with an example, how to use HyperLinkField (HyperLink) in GridView in ASP.Net using C# and VB.Net.
 
 

HTML Markup

Page1

The HTML Markup consists of following control:
GridView – For displaying data.

Columns

The GridView consists of two BoundField columns and one HyperLinkField column.
The HyperLinkField column has been assigned with the following properties:
DataTextField – The name of the column from the data source which will be displayed.
DataNavigateUrlFields – The column name which data will be passed as QueryString to another page.
DataNavigateUrlFormatString – For navigating to another page with QueryString value.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"> 
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:HyperLinkField DataTextField="Name" DataNavigateUrlFields="CustomerId" HeaderText="Name"
            DataNavigateUrlFormatString="~/DetailsCS.aspx?CustomerId={0}" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 

Page2

The HTML Markup consists of following control:
Label – For displaying CustomerId of the selected customer.
Customer Id:
<asp:Label ID=" lblId runat="server"  Font-Bold="true"></asp:Label
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 

Binding the GridView using C# and VB.Net

Inside the Page_Load event handler, the GridView is populated with dynamic DataTable.
Note: For more details on how to populate GridView with dynamic DataTable, please refer my article Dynamically create DataTable and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object  sender,EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable  dt =  new DataTable();
        dt.Columns.AddRange(new DataColumn[3] {
            new DataColumn("CustomerId"),
            new DataColumn("Name"),
            new DataColumn("Country")
        });
        dt.Rows.Add(1,"John Hammond","United States");
        dt.Rows.Add(2,"Mudassar Khan","India");
        dt.Rows.Add(3,"Suzanne Mathews","France");
        dt.Rows.Add(4,"Robert Schidner","Russia");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)Handles Me.Load
    If Not Me.IsPostBack Then
        Dim  dt As DataTable  =  New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {
                            New DataColumn("CustomerId"),
                            New DataColumn("Name"),
                            New DataColumn("Country")})
        dt.Rows.Add(1,"John Hammond","United States")
        dt.Rows.Add(2,"Mudassar Khan","India")
        dt.Rows.Add(3,"Suzanne Mathews","France")
        dt.Rows.Add(4,"Robert Schidner","Russia")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End If
End Sub
 
 

Displaying CustomerId of the selected Customer

Inside the Page_Load event handler, the CustomerId is fetched from the QueryString and displayed using Label control.
C#
protected void Page_Load(object  sender,EventArgs e)
{
    lblId.Text Request.QueryString["CustomerId"];
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)Handles Me.Load
    lblId.Text Request.QueryString("CustomerId")
End Sub
 
 

Screenshot

HyperLinkField ( HyperLink ) in ASP.Net GridView Example
 
 

Demo

 
 

Downloads