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
ataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="ID" DataNavigateUrlField="ID"
DataNavigateUrlFormatString="link.aspx?id={0}"
DataTextField="ID"></asp:HyperLinkColumn>
</Columns>
</asp
ataGrid>
So in case of just displaying the Website we would normally write it as:
Method 2:
<asp
ataGrid 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
ataGrid>
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
ataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="http://{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp
ataGrid>
In this case none of the above given Methods would do the task. The
workaround is use TemplateColumn and Helper function
Method 4:
<asp
ataGrid 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
ataGrid>
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