Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net GridView get row index when row clicked

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

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


Thanks
Image1.jpg
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia 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
Avatar of Murray Brown

ASKER

Thanks very much