Example E308
Visible to All Users

Grid View for ASP.NET Web Forms - How to create and configure a HyperLink column at runtime

This example demonstrates how to create a grid column that displays hyperlinks.

Hyperlink Column

Follow the steps below to create the HyperLink column at runtime.

  1. Create an object of the GridViewDataHyperLinkColumn type.
  2. Use the object's PropertiesHyperLinkEdit property to customize the hyperlink-related settings.
  3. Call the Add or Insert method to add the newly created column to the grid column collection.
C#
public void PopulateColumns() { // ... GridViewDataHyperLinkColumn colItemName = new GridViewDataHyperLinkColumn(); colItemName.FieldName = "ItemName"; colItemName.PropertiesHyperLinkEdit.NavigateUrlFormatString = "~/details.aspx?Device={0}"; colItemName.PropertiesHyperLinkEdit.TextFormatString = "Get details about device {0}"; colItemName.PropertiesHyperLinkEdit.TextField = "ItemName"; ASPxGridView1.Columns.Add(colItemName); }

Columns are created on the first Page_Init event call. Then, the grid automatically recreates columns from the view state on a callback or post back.

C#
protected void Page_Init(object sender, EventArgs e) { ASPxGridView1.KeyFieldName = "ID"; ASPxGridView1.DataSource = GetData(); if (!IsPostBack && !IsCallback) { PopulateColumns(); ASPxGridView1.DataBind(); } }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Default.aspx.cs(vb)
C#
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using DevExpress.Web; namespace HyperlinkColumn { public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { ASPxGridView1.KeyFieldName = "ID"; ASPxGridView1.DataSource = GetData(); if (!IsPostBack && !IsCallback) { PopulateColumns(); ASPxGridView1.DataBind(); } } public DataTable GetData() { DataTable Table = new DataTable(); Table.Columns.Add("ID", typeof(int)); Table.Columns.Add("ItemName", typeof(string)); Table.Rows.Add(1, "A"); Table.Rows.Add(2, "B"); return Table; } public void PopulateColumns() { GridViewDataTextColumn colID = new GridViewDataTextColumn(); colID.FieldName = "ID"; ASPxGridView1.Columns.Add(colID); GridViewDataHyperLinkColumn colItemName = new GridViewDataHyperLinkColumn(); colItemName.FieldName = "ItemName"; colItemName.PropertiesHyperLinkEdit.NavigateUrlFormatString = "~/details.aspx?Device={0}"; colItemName.PropertiesHyperLinkEdit.TextFormatString = "Get details about device {0}"; colItemName.PropertiesHyperLinkEdit.TextField = "ItemName"; ASPxGridView1.Columns.Add(colItemName); } } }
Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HyperlinkColumn._Default" %> <%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxwgv" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server"> </dxwgv:ASPxGridView> </div> </form> </body> </html>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.