GridView is a powerful and easy in use tool in, especially if you know how to use it. There are a lot of tutorials how to add dynamic hyperlink column to GridView not programmatically, but this tutorial is about how to add hyperlink to GridView programmatically (in code-behind).

Initial data:

You have a class CEvent and collection List<CEvent>:


And you want to have a column in your GridView with a hyperlink of this format:



In a lot of tutorials it’s written how to add hyperlink column in GridView control via designer in .aspx page. They look somehow like this:


It’s a good way. But there are a lot of cases, when you have to write GridView properties and add columns from code-behind. It wasn’t very easy for me at first.

I’ve decided to describe HyperLinkField equivalent to .aspx syntax:


But I’ve got this error:

Error      2             Cannot implicitly convert type 'string' to 'string[]'

The reason is that value for "DataNavigateUrlFields" must be a string array, that’s why I had to change some code. One of correct ways to add hyperlink to GridView programmatically is below: