Gridview and Dynamically making a hyperlink....

I populate a dataset and bind it to a gridview...
            grd1.DataSource = dsXXX
            grd1.DataBind()

Then within the RowDataBound event I try to make a field within a specific column a hyperlink by using the following code....

                Dim hLink As HyperLink = TryCast(e.Row.Cells(2).Controls(0), HyperLink)
                hLink.NavigateUrl = "Profile.aspx?q=" + e.Row.Cells(2).Text

Unfortunatly I receive the following error:

               "Specified argument was out of the range of valid values. Parameter name: index"

Can anyone help me make a hyperlink when dynamically binding a dataset?
Thanks!


jsegAsked:
Who is Participating?
 
Kiran SonawaneProject LeadCommented:
Try this

Dim hLink As HyperLink = DirectCast(e.Row.FindControl("your_hiperlink_id"), HyperLink)     ' replace your_hiperlink_id with actual id
         hLink.NavigateUrl = "Profile.aspx?q=" + e.Row.Cells(2).Text
0
 
guveraCommented:
Hi,

 That error basically means that you are specifying an integer value for a Cell() that doesn't exist. In other words, there aren't as many cells as the code wants to get data from. Keep in mind that the Cells() collection is a 0-based index, so if you have a total of 15 cells, you would have to get data from Cells(0) to Cells(14). Trying to get something from Cells(15) would generate this type of error.

Regards
Guvera
0
 
jsegAuthor Commented:
I auto generate the columns within the grid.   e.Row.Cells(2).Text  is my third column.....  I need to make a hyperlink when dynamically binding a dataset.

Thanks
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Tom BeckCommented:
>>I need to make a hyperlink when dynamically binding a dataset

Make a hyperlink out of what? What control type is currently in Cells(2)? A Label?
0
 
jsegAuthor Commented:
Read the first line....gridview
0
 
Tom BeckCommented:
You are trying to cast the first control in the third column of a row in your GridView into a HyperLink. So, I ask you what control type is currently in that position and your answer is GridView? That doesn't make sense.
0
 
CodeCruiserCommented:
Try changing this

Dim hLink As HyperLink = TryCast(e.Row.Cells(2).Controls(0), HyperLink)
                hLink.NavigateUrl = "Profile.aspx?q=" + e.Row.Cells(2).Text

to


Dim hLink As New HyperLink
hLink.NavigateUrl = "Profile.aspx?q=" + e.Row.Cells(2).Text
hLink.Text = e.Row.Cells(2).Text
e.Row.Cells(2).Controls.Add(hLink)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.