This example demonstrates how to create a grid column that displays hyperlinks.
Follow the steps below to create the HyperLink column at runtime.
- Create an object of the GridViewDataHyperLinkColumn type.
- Use the object's PropertiesHyperLinkEdit property to customize the hyperlink-related settings.
- 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
- Default.aspx.cs (VB: Default.aspx.vb)
- Default.aspx (VB: Default.aspx)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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);
}
}
}
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>