Home > DeveloperSection > Forums > GridView to add HyperLink column dynamically
Ashish Pandey

Total Post:128

Points:898
Posted on    August-31-2014 11:59 PM

 C#
Ratings:


 1 Reply(s)
 1097  View(s)
Rate this:

We read from XML and create columns in asp gridview. Also same XML is used to create columns in a data table.

We populate that data table with desired data and bind data table to gridview using server side code.

I want to display hyperlink in gridview and not sure how to manage it. Since we are not using RowDataBound method.

Code 

foreach (XmlNode columnNode in columnNodes)

{

dc = new DataColumn(columnNode.Attributes["ColumnName"].Value,Type.GetType("System.String"));

dt.Columns.Add(dc);

boundField = new BoundField();

boundField.HeaderText = columnNode.Attributes["ColumnDisplayName"].Value;

boundField.DataField = columnNode.Attributes["ColumnName"].Value;

boundField.SortExpression = columnNode.Attributes["ColumnName"].Value;

grdView.Columns.Add(boundField);

}

where dc is datacolumn, dt is data table and grdView is grid view.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-01-2014 12:34 AM

Hi Ashish, 
try this:

linkField = new HyperLinkField();

linkField.HeaderText = columnNode.Attributes["ColumnDisplayName"].Value;

// The field you want to use as the displayed text of the hyperlink

linkField.DataTextField = columnNode.Attributes["ColumnName"].Value;

// The field(s) you want to use in the URL behind the hyperlink

linkField.DataNavigateUrlFields = new string[] { columnNode.Attributes["ColumnName"].Value };

// The formatting string for your hyperlink.  Use this to build the links the way you want them.

linkField.DataNavigateUrlFormatString = "http://yourSiteName/links/{0}";

linkField.SortExpression = columnNode.Attributes["ColumnName"].Value;

grdView.Columns.Add(linkField);



Don't want to miss updates? Please click the below button!

Follow MindStick