Example E293
Visible to All Users

Grid View for ASP.NET Web Forms - Create a DataItemTemplate for a column at Runtime

This example demonstrates how to create a column cell template at runtime. Grid cells display the ASPxHyperLink control created in the template.

ASPxGridView-DataItemTemplate

Implementation Details

Declare a custom class that implements the ITemplate interface.

In the class's InstantiateIn method, create controls that make up the template and add them to the control collection of a container passed as this method's parameter. The container type is different for each template type. For a DataItemTemplate object, the container is of the GridViewDataItemTemplateContainer type. Use the container's properties to obtain a row's information. For example, use the KeyValue property to get a row key (container.KeyValue).

Create an instance of the template class and assign it to the column's DataItemTemplate property.

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

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

Example Code

HyperlinkColumn/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HyperlinkColumn._Default" %> <%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!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> <dx:ASPxGridView ID="ASPxGridView1" runat="server" OnLoad="ASPxGridView1_Load"> </dx:ASPxGridView> </div> </form> </body> </html>
HyperlinkColumn/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_Load(object sender, EventArgs e) { } protected void ASPxGridView1_Load(object sender, EventArgs e) { ASPxGridView1.KeyFieldName = "ID"; ASPxGridView1.DataSource = GetData(); if(!IsPostBack && !IsCallback) { PopulateColumns(); ASPxGridView1.DataBind(); } else CreateTemplate(); } public DataTable GetData() { DataTable Table = new DataTable(); Table.Columns.Add("ID", typeof(int)); Table.Rows.Add(1); Table.Rows.Add(2); return Table; } public void PopulateColumns() { GridViewDataTextColumn colID = new GridViewDataTextColumn(); colID.FieldName = "ID"; ASPxGridView1.Columns.Add(colID); GridViewDataTextColumn colItemTemplate = new GridViewDataTextColumn(); colItemTemplate.DataItemTemplate = new MyHyperlinkTemplate(); // Create a template colItemTemplate.Name = "colItemTemplate"; colItemTemplate.Caption = "Template Column"; ASPxGridView1.Columns.Add(colItemTemplate); } private void CreateTemplate() { ((GridViewDataColumn)ASPxGridView1.Columns["colItemTemplate"]).DataItemTemplate = new MyHyperlinkTemplate(); } } class MyHyperlinkTemplate : ITemplate { public void InstantiateIn(Control container) { ASPxHyperLink link = new ASPxHyperLink(); GridViewDataItemTemplateContainer gridContainer = (GridViewDataItemTemplateContainer)container; link.NavigateUrl = string.Format("~/details.aspx?Device={0}", gridContainer.KeyValue); link.Text = string.Format("Get details about device {0}", gridContainer.KeyValue); container.Controls.Add(link); } } }

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.