473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Hyperlink column in Datatable???

I currently have the following datagrid but want to turn the name and email
column into a hyperlink in the codebehind!

Can someone please tell me how I achieve this!

Thanks

Private Sub createTable()
Dim tbcontacts As DataTable = New DataTable("contacts")
tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Name", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Dept.", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Email", System.Type.GetType("System.String"))
ds.Tables.Add(tbcontacts)
End Sub
Nov 19 '05 #1
3 15370
Hi Tim,

The HyperLinkColumn in case of displaying the ID's as link on displaying the
details information would be done as given below:

Method 1:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="ID" DataNavigateUrlField="ID"
DataNavigateUrlFormatString="link.aspx?id={0}"
DataTextField="ID"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>
So in case of just displaying the Website we would normally write it as:

Method 2:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

Issue:

Using method 2 we would get the link which would be like
"http://servername/WebArticles/DataGrid/www.asp.net"

Note: If the Website Column has value as "http://www.asp.net" the Property
DataNavigateUrlFormatString="{0}" would give the proper result

But in our case the website values are "www.asp.net" so the way to handle
this is: Method 3:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="http://{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

In this case none of the above given Methods would do the task. The
workaround is use TemplateColumn and Helper function

Method 4:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Link">
<ItemTemplate>
<asp:HyperLink Runat =server
NavigateUrl ='<%#GetURL(DataBinder.Eval(Container.DataItem, "Link"))%>' >
<%#DataBinder.Eval(Container.DataItem, "Link")%>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</asp:DataGrid>
Here GetURL is the helper function

Function GetURL (ByVal fldval As String) As String
If InStr(fldval, "http://")Then
Return fldval
Else
Return "http://" & fldval
End If
End Function
The code to bind data to DataGrid would be as.....

Private ds As DataSet = New DataSet()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
binddata()
End If
End Sub

Sub binddata()
Dim sqlStmt As String = "Select * from Documentlink "
Dim conString As String =
"server=localhost;database=Northwind;uid=sa;pw d=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:43**********************************@microsof t.com...
I currently have the following datagrid but want to turn the name and email
column into a hyperlink in the codebehind!

Can someone please tell me how I achieve this!

Thanks

Private Sub createTable()
Dim tbcontacts As DataTable = New DataTable("contacts")
tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Name",
System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Dept.",
System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Email",
System.Type.GetType("System.String"))
ds.Tables.Add(tbcontacts)
End Sub

Nov 19 '05 #2
Thank you for the reply but this isn't really what I asked for...

I want to know how to create a hyperlink column in the codebehind...

ie: How do I create a hyperlink column programatically in a datatable???

Thank you for the response...

"Mona" wrote:
Hi Tim,

The HyperLinkColumn in case of displaying the ID's as link on displaying the
details information would be done as given below:

Method 1:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="ID" DataNavigateUrlField="ID"
DataNavigateUrlFormatString="link.aspx?id={0}"
DataTextField="ID"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>
So in case of just displaying the Website we would normally write it as:

Method 2:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

Issue:

Using method 2 we would get the link which would be like
"http://servername/WebArticles/DataGrid/www.asp.net"

Note: If the Website Column has value as "http://www.asp.net" the Property
DataNavigateUrlFormatString="{0}" would give the proper result

But in our case the website values are "www.asp.net" so the way to handle
this is: Method 3:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="http://{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

In this case none of the above given Methods would do the task. The
workaround is use TemplateColumn and Helper function

Method 4:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Link">
<ItemTemplate>
<asp:HyperLink Runat =server
NavigateUrl ='<%#GetURL(DataBinder.Eval(Container.DataItem, "Link"))%>' >
<%#DataBinder.Eval(Container.DataItem, "Link")%>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</asp:DataGrid>
Here GetURL is the helper function

Function GetURL (ByVal fldval As String) As String
If InStr(fldval, "http://")Then
Return fldval
Else
Return "http://" & fldval
End If
End Function
The code to bind data to DataGrid would be as.....

Private ds As DataSet = New DataSet()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
binddata()
End If
End Sub

Sub binddata()
Dim sqlStmt As String = "Select * from Documentlink "
Dim conString As String =
"server=localhost;database=Northwind;uid=sa;pw d=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:43**********************************@microsof t.com...
I currently have the following datagrid but want to turn the name and email
column into a hyperlink in the codebehind!

Can someone please tell me how I achieve this!

Thanks

Private Sub createTable()
Dim tbcontacts As DataTable = New DataTable("contacts")
tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Name",
System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Dept.",
System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Email",
System.Type.GetType("System.String"))
ds.Tables.Add(tbcontacts)
End Sub


Nov 19 '05 #3
Hi Tim,

The hyperlink columns cannot be set through the datatable, as the datatable is
your object that should contain all the relevant data which you then manipulate
and present to the end user. If you want hyperlink columns, you should do this
through the datagrid, since it is the presentation component for your application.
You can do this using the method I showed you in my previous email

HTH

Mona[Grapecity]
"Tim::.." <myatix_at_hotmail.com> wrote in message news:5C**********************************@microsof t.com...
Thank you for the reply but this isn't really what I asked for...

I want to know how to create a hyperlink column in the codebehind...

ie: How do I create a hyperlink column programatically in a datatable???

Thank you for the response...



"Mona" wrote:
Hi Tim,

The HyperLinkColumn in case of displaying the ID's as link on displaying the
details information would be done as given below:

Method 1:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="ID" DataNavigateUrlField="ID"
DataNavigateUrlFormatString="link.aspx?id={0}"
DataTextField="ID"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>


So in case of just displaying the Website we would normally write it as:

Method 2:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

Issue:

Using method 2 we would get the link which would be like
"http://servername/WebArticles/DataGrid/www.asp.net"

Note: If the Website Column has value as "http://www.asp.net" the Property
DataNavigateUrlFormatString="{0}" would give the proper result

But in our case the website values are "www.asp.net" so the way to handle
this is: Method 3:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="http://{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

In this case none of the above given Methods would do the task. The
workaround is use TemplateColumn and Helper function

Method 4:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Link">
<ItemTemplate>
<asp:HyperLink Runat =server
NavigateUrl ='<%#GetURL(DataBinder.Eval(Container.DataItem, "Link"))%>' >
<%#DataBinder.Eval(Container.DataItem, "Link")%>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</asp:DataGrid>


Here GetURL is the helper function

Function GetURL (ByVal fldval As String) As String
If InStr(fldval, "http://")Then
Return fldval
Else
Return "http://" & fldval
End If
End Function


The code to bind data to DataGrid would be as.....

Private ds As DataSet = New DataSet()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
binddata()
End If
End Sub

Sub binddata()
Dim sqlStmt As String = "Select * from Documentlink "
Dim conString As String =
"server=localhost;database=Northwind;uid=sa;pw d=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:43**********************************@microsof t.com...
>I currently have the following datagrid but want to turn the name and email
> column into a hyperlink in the codebehind!
>
> Can someone please tell me how I achieve this!
>
> Thanks
>
> Private Sub createTable()
> Dim tbcontacts As DataTable = New DataTable("contacts")
> tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Name",
> System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Dept.",
> System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Email",
> System.Type.GetType("System.String"))
> ds.Tables.Add(tbcontacts)
> End Sub



Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ravikanth[MVP] | last post by:
Hi asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="Sample Column"> <ItemTemplate> <asp:Hyperlink runat="server" Text='<%...
9
by: Paul | last post by:
Hi I have a data grid with a hyperlink column. the colum has numbers like 00001,000002, ect. Just wondering how to get the text value of the cell as tempstring =...
1
by: D. Shane Fowlkes | last post by:
Hello All. I keep asking for help with this on the www.asp.net forums and nobody seems to be able to help. What I'm trying to accomplish is very simple. I simply want to create a Hyperlink...
0
by: Tim::.. | last post by:
Can someone please tell me how I create a hyperlink column in a datatable... I have the following Sub that creates a datatable from my Active Directory but I would like to be able to turn the...
2
by: Jason | last post by:
I have a data grid with a hyperlink column. The hyperlink is created by a class that extracts the link from an XML Document. How can I populate the hyperlink column in the data grid with the...
0
by: Waran | last post by:
Hi, In a datagrid I am displaying 2 columns. One is a Hyperlink Colum and the other is a Button Column. The values for the hyperlink columns are fetched from the database and its a url. When I...
10
by: david | last post by:
Hi, all: I need a help from you about DataGrid control. I created a DataGrid, dg, in design view of .NET visual Stadio and use the builder to add a Hyperlink column to dg. I want to try to assign...
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
3
by: William LaMartin | last post by:
I have a gridview (with no properties set) on an aspx page which I populate from an XML file with the code below. The data in the XML file looks like this <description>National Trust for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.