Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Gridview and Dynamically making a hyperlink....

Posted on 2011-10-12
7
Medium Priority
?
349 Views
Last Modified: 2012-05-12
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!


0
Comment
Question by:jseg
7 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 2000 total points
ID: 36956010
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
 
LVL 8

Expert Comment

by:guvera
ID: 36956016
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
 

Author Comment

by:jseg
ID: 36956214
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Expert Comment

by:Tom Beck
ID: 36957819
>>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
 

Author Comment

by:jseg
ID: 36958490
Read the first line....gridview
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36958660
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36961010
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question