troubleshooting Question

ASP.net GridView get row index when row clicked

Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland asked on
ASP.NETVisual Basic.NET
2 Comments1 Solution152 ViewsLast Modified:
Hi

In ASP.net if a user clicks on a GridView row what VB.net code do I use to get the row index?
I currently use the code below but if there is only one row in the GridView then it doesn't run

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
        Try
            Dim oMessageNumber As String
            Dim oSelectedRowIndex As String = Me.GridView1.SelectedRow.RowIndex

            oMessageNumber = Me.GridView1.Rows(oSelectedRowIndex).Cells(0).Text

            Response.Redirect("Messages")

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

I tried adding the following code but got the error shown in the image attached

    Private Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated

        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(Me.GridView1, "Select$" & e.Row.RowIndex.ToString()))
            e.Row.Style.Add("cursor", "pointer")
        End If

    End Sub

Thanks
Image1.jpg
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros