troubleshooting Question 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:

In if a user clicks on a GridView row what 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
            Dim oMessageNumber As String
            Dim oSelectedRowIndex As String = Me.GridView1.SelectedRow.RowIndex

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


        Catch ex As Exception
        End Try
    End Sub

Open in new window

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

Open in new window

Robberbaron (robr)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
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