Thursday, October 22, 2015

How to emulate a DataItemTemplate effect when adding a column to a DevExpress ASPxGridView at the C# side.

Per this you may add a hyperlink at a column, sort of like this one (but without the conditional logic for this example), like so:

protected void AddLinkColumn(string name, string caption)
{
   var column = new GridViewDataTextColumn
   {
      FieldName = dbName,
      Caption = caption
   };
   column.DataItemTemplate = new HyperlinkTemplate();
   MyGrid.Columns.Add(column);
}

 
 

We need an implementation of ITemplate like so:

using System.Web.UI;
using DevExpress.Web;
namespace Whatever
{
   public class HyperlinkTemplate : ITemplate
   {
      public void InstantiateIn(Control container)
      {
         ASPxHyperLink link = new ASPxHyperLink();
         GridViewDataItemTemplateContainer gridContainer =
               (GridViewDataItemTemplateContainer)container;
         link.NavigateUrl = Utilities.GetBaseLinkUrl() + gridContainer.KeyValue;
         link.Text = gridContainer.Text;
         container.Controls.Add(link);
      }
   }
}

 
 

.KeyValue is going to use the unique key for the recordset for the grid as defined by...

MyGrid.KeyFieldName = "WhateverId";

 
 

...while .Text is the value that would typically display for the cell at hand for the column.

No comments:

Post a Comment