Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Putting links into a gridview dynamically (.net 4.5.1)

Putting links into a gridview dynamically (.net 4.5.1)

Putting links into a gridview dynamically (.net 4.5.1)

I have an application which was written in .NET 1.1 that used a datagrid that I would dynamically change a cell into a link based on the column and security settings. For example:

Column is an ID field and user has access to the record - Show a link.
Column is an ID field, and user does not have access to the recod - Don't show the link (normal text).
Column is not an ID field - Don't show the link (normal text).

I'm attempting to create a user control for .NET 4.5 (First major project using 4.5, and there's definately a lot of different, and nice features added, so it's a steep learning curve).

I've read that gridviews are the successors to datagrids, and I'm trying to create a usercontrol based on the gridview. Anyway, my problem is that when I change the text to a link in a gridview, it displays the HTML code (the HTML source shows <, &rt;, etc.). I even tried &rt;, < in the code, and it didn't work. However, with the datagrid, links would work just fine. (It's not just links, but any HTML code, like making text bold) Is there a way to make the HTML show up in an individual cell?

I know there are linkcolumns that I can add, but the ID field isn't always known, if it exists at all (as the control is used in multiple webforms).

This is some test code -


<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DataGrids.aspx.vb" Inherits="WebApplication1.DataGrids" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />

      <br />
      <strong>DataGrid</strong><asp:datagrid id="dgDataGrid" runat="server">

      <br />
      <strong>GridView</strong><asp:GridView ID="gvGridView" runat="server">


Public Class DataGrids
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim db As New dbData  ' Temporary class name from old application with hideiously non-refactored code. :)

    Dim dt As DataTable = db.getDataTable("SELECT 'Test' as 'ID_Column'", db.InventoryConnection)

    Dim dv As New DataView(dt)

    For i As Integer = 0 To dv.Table.Rows.Count - 1

      If dv.Table.Columns(0).ColumnName = "ID_Column" Then
        dv.Table.Rows(i).Item(0) = "<a href='http://www.google.com' target='_blank'>Google</a>"
      End If


    dgDataGrid.DataSource = dv
    gvGridView.DataSource = dv


  End Sub

End Class 

RE: Putting links into a gridview dynamically (.net 4.5.1)

IF you don't want the actual HTML markup to show, you have to HTMLEncode() the text.

RE: Putting links into a gridview dynamically (.net 4.5.1)

I did manage to find a work-around for what I want, via modifying the gridview after it has been databound. I also found and slightly modified some code I found on the Internet (the original assumed that the column would be found, so I added a boolean to check if that was the case or not. If not, return "Nothing").


    For i As Integer = 0 To gvGridView.Rows.Count - 1
      Dim id As String = GetColumnIndexByName(gvGridView.Rows(i), "ID_Column")

      If Not id = Nothing Then
        gvGridView.Rows(i).Cells(id).Text = "<a href='http://www.google.com' target='_blank'>Google</a>"

      End If

  End Sub

  Function GetColumnIndexByName(row As GridViewRow, columnName As String) As Object

    Dim columnIndex As Integer = 0
    Dim found As Boolean = False

    For Each cell As DataControlFieldCell In row.Cells

      If (cell.ContainingField.HeaderText = columnName) Then
        found = True
        Exit For
      End If
      columnIndex += 1 ' keep adding 1 while we don't have the correct name


    If found Then
      Return columnIndex
    End If
    Return Nothing
  End Function 

RE: Putting links into a gridview dynamically (.net 4.5.1)

To add, I'm not sure if it's the best practice, but it works.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close