want to make column in DataGrid a Hyperlink

H

Henry Jones

I am new to ASP.NET and I'm using Visual Studio 2003. I put a datagrid on a
form and bound to the Northwind/Orders table. The first column is the ID
field. How can I make the ID field display as a hyperlink on the grid when
the web form is displayed?

Thanks
 
K

Ken Cox [Microsoft MVP]

Hi Henry,

You need to use a template column. See the code below to give you the idea.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<script runat="server">

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("URLvalue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i.ToString & ".aspx"
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i <> 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
dg3.DataSource = CreateDataSource()
dg3.DataBind()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Check a checkbox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid id="dg3" runat="server" autogeneratecolumns="False">
<columns>
<asp:templatecolumn headertext="Link">
<itemtemplate>
<asp:hyperlink runat="server" navigateurl='<%#
DataBinder.Eval(Container, "DataItem.URLvalue") %>' target="_blank"
text='<%# DataBinder.Eval(Container, "DataItem.StringValue")
%>'></asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</div>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top