want to make column in DataGrid a Hyperlink

Discussion in 'ASP .Net Datagrid Control' started by Henry Jones, Jul 22, 2006.

  1. Henry Jones

    Henry Jones Guest

    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
     
    Henry Jones, Jul 22, 2006
    #1
    1. Advertising

  2. 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>


    "Henry Jones" <> wrote in message
    news:...
    >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
    >
     
    Ken Cox [Microsoft MVP], Jul 23, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2FyYW4=?=

    HyperLink Column and Button Column in a DataGrid

    =?Utf-8?B?V2FyYW4=?=, Aug 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    627
    =?Utf-8?B?V2FyYW4=?=
    Aug 9, 2005
  2. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    11
    Views:
    1,028
    deandany
    Jul 17, 2006
  3. davetichenor
    Replies:
    1
    Views:
    910
    Eliyahu Goldin
    Oct 30, 2006
  4. Keith G Hicks
    Replies:
    3
    Views:
    807
    Keith G Hicks
    Feb 18, 2008
  5. Dave
    Replies:
    0
    Views:
    1,000
Loading...

Share This Page