How to create hyperlink in datagrid at runtime

Tom Chesley

Member
Joined
Jun 18, 2006
Messages
12
Programming Experience
1-3
I want to create a hyperlink in my datagrid at runtime because I will be changing my select according to what the user wishes to see.
included here is what i have so far witch functions well displaying correct information, but in the datagrid it doesnt do the hyperling like it doe here.
An example of the db is

Track_Name Author File_Name URL EVO_Version Track_Type
Dakar2 Tonka Dakar2.pod http://tonkas.no-ip.org/evo1/Dakar2.pod EVO1 Rally Race

Where do i go from here? Thank you in advance
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#008000]'Put user code to initialize the page here[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SELcmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "SELECT Track_Name, Author, File_Name, URL, EVO_Version, Track_Type FROM Tracks"[/SIZE]
[SIZE=2]Do_Select(SELcmd)[/SIZE]
[SIZE=2]dg.Caption = "Show Everything"[/SIZE]
[SIZE=2]dg.DataBind()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Do_Select([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] cmd)[/SIZE]
[SIZE=2][COLOR=#008000]'create SQL command[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] selCMD [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlCommand(cmd)[/SIZE]
[SIZE=2][COLOR=#008000]'create data adapter[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] daTrack [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlDataAdapter(selCMD)[/SIZE]
[SIZE=2][COLOR=#008000]'set the connector to the adapter[/COLOR][/SIZE]
[SIZE=2]daTrack.SelectCommand.Connection = con[/SIZE]
[SIZE=2][COLOR=#008000]'create dataset[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ds [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'fill the data adapter[/COLOR][/SIZE]
[SIZE=2]daTrack.Fill(ds, "Tracks")[/SIZE]
[SIZE=2][COLOR=#008000]'bind the datagrid[/COLOR][/SIZE]
[SIZE=2]dg.DataSource = ds[/SIZE]
[SIZE=2]dg.DataMember = "Tracks"[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]lblMsg.Text = ex.Message[/SIZE]
[SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
[SIZE=2]con.Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnEVO1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnEVO1.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SELcmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "SELECT Track_Name, Author, URL, EVO_Version, Track_Type FROM Tracks "[/SIZE]
[SIZE=2]SELcmd &= "WHERE EVO_Version = 'EVO1'"[/SIZE]
[SIZE=2]Do_Select(SELcmd)[/SIZE]
[SIZE=2]dg.Caption = "Show Evo 1 Only"[/SIZE]
[SIZE=2]dg.DataBind()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
I now can do a hyperlink using the property builder, but it shows up as the first column, i dont mind that for this database, but then its listed twice in the same row, once as the hyperlink, and once as text. I tried
dg.Columns(2).Visible = False
before and after the dg.Databind()
I then get index out of range
I would prefer the hyperlink be the URL column but either way will do the trick for me...
 
I figured out a workaround, probably not the cleanest way to do it, but works
I added a label not visible to the form, then set that label to the row number I want to remove, I then had to add an Itemcreated instance for the datagrid

This will work for me, but would have been nice to have the URL in the column I wanted it in the first place.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] dg_ItemCreated([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.DataGridItemEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] dg.ItemCreated[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] col [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2]col = Val(lblCol.Text)[/SIZE]
[SIZE=2]e.Item.Cells(col).Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

 
Back
Top