Hyperlink column in Datatable???

G

Guest

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
 
M

Mona

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;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]
 
G

Guest

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 said:
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;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]

Tim::.. said:
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
 
M

Mona

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::.. said:
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 said:
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;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]

Tim::.. said:
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
 
Joined
Feb 23, 2010
Messages
2
Reaction score
0
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:[email protected]...
> 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;pwd=;"
>> 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:[email protected]...
>> >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

>>
>>
>>

Actually you can determine how a datagrid will look and behave based on the Type of the datatable columns. A type of string behaves differently than a type of double or bool. A bool datacolumn will show as a checkbox when the grid renders.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top