Link to home
Start Free TrialLog in
Avatar of jseg
jsegFlag for United States of America

asked on

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!


ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of jseg

ASKER

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
>>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?
Avatar of jseg

ASKER

Read the first line....gridview
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.
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)