Link to home
Start Free TrialLog in
Avatar of rcl58
rcl58

asked on

Gridview RowEditing Can't FindControl V2

Oops, I accepted a solution that is not working , same problem. ( https://www.experts-exchange.com/questions/28017315/Gridview-RowEditing-Can't-FindControl.html?anchorAnswerId=38859213#a38859213 )

When entering Edit mode on my GridView I place the focus on a field with the FindControl method. All is well, but if I move to page #2 my code crashes, (ArgumentOutOfRangeException).

Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

        If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex
        gv_Gifts.DataBind()
        gv_Gifts.Rows(e.NewEditIndex).FindControl("giftTitle").Focus()

    End Sub

Open in new window


After going to page 2 and entering edit on the first row
e.NewEditIndex = 10
.PageSize = 10
.PageIndex = 1

Help?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

So what happens when you incorporate the change suggested in your previous question?
Avatar of rcl58
rcl58

ASKER

Same ArgumentOutOfRangeException exeption...

correctIndex = e.NewEditIndex + (GridView1.PageSize * GridView1.PageIndex)" resulted in a value of 20.
Avatar of rcl58

ASKER

Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

        If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex

        LoadOffers() 
        'NOTE: This queries db, creates dataset, assigns table to  gv_Gifts.DataSource, and binds
 
        Dim correctIndex As Integer 
        correctIndex = e.NewEditIndex - (gv_Gifts.PageSize * gv_Gifts.PageIndex)

        gv_Gifts.Rows(correctIndex).FindControl("giftTitle").Focus()

    End Sub 

Open in new window

Ok something strange is going on. Again it works on the first page.

GRID PAGE ONE (second record down)
Private Sub gv_Gifts_RowEditing...
e.NewEditIndex........1
e.PageSize...........10
e.PageIndex...........0
gv.Rows.Count........10

GRID PAGE TWO (second record down)
Private Sub gv_Gifts_RowEditing...
 e.NewEditIndex......11
 e.PageSize..........10
 e.PageIndex..........1
 gv.Rows.Count........3
 
 Hence, correctIndex = e.NewEditIndex - ( gv_Gifts.PageSize *  gv_Gifts.PageIndex)
 
 GRID PAGE ONE
  gv_Gifts.Rows(1).FindControl("giftTitle").Focus() - WORKS
 
 GRID PAGE TWO
  gv_Gifts.Rows(1).FindControl("giftTitle").Focus() - FAIL

ALSO...

Entering edit mode with a double click.

 Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

        For Each row As GridViewRow In gv_Gifts.Rows
            If row.RowType = DataControlRowType.DataRow Then

                If gv_Gifts.EditIndex <> row.DataItemIndex Then

                 row.Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperlink(gv_Gifts, "Edit$" & row.DataItemIndex, True)

                End If
            End If
        Next

        MyBase.Render(writer)
    End Sub

Open in new window

So it fails with

correctIndex = e.NewEditIndex - ( gv_Gifts.PageSize *  gv_Gifts.PageIndex)

and

correctIndex = e.NewEditIndex + ( gv_Gifts.PageSize *  gv_Gifts.PageIndex)


>GRID PAGE TWO (second record down)
?Private Sub gv_Gifts_RowEditing...
>e.NewEditIndex......11

Is that neweditindex 11 before the calculation? It should be 1 on page 2.
Avatar of rcl58

ASKER

Yes it fails with both "+" and "-" in the calculation. Yes it is 11 before the calculation.

Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing

 If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex
        LoadOffers()
     
        Diagnostics.Debug.WriteLine(" e.NewEditIndex..........." & e.NewEditIndex.ToString)
        Diagnostics.Debug.WriteLine(" e.PageSize..........." & gv_Gifts.PageSize.ToString)
        Diagnostics.Debug.WriteLine(" e.PageIndex..........." & gv_Gifts.PageIndex.ToString)
        Diagnostics.Debug.WriteLine(" .Rows.Count.........." & gv_Gifts.Rows.Count.ToString)

        Dim correctIndex As Integer = e.NewEditIndex - (gv_Gifts.PageSize * gv_Gifts.PageIndex)

        Diagnostics.Debug.WriteLine(" correctIndex ..........." & correctIndex.ToString)

        gv_Gifts.Rows(correctIndex ).FindControl("giftTitle").Focus() 'Fails Here on Page Two

Open in new window

GRID PAGE ONE (second record down)
e.NewEditIndex........1
 e.PageSize...........10
 e.PageIndex...........0
 .Rows.Count..........10
 correctIndex .........1
 
 GRID PAGE TWO (second record down)
 e.NewEditIndex.......11
 e.PageSize...........10
 e.PageIndex...........1
 .Rows.Count...........3
 correctIndex .........1


Also, LoadOffers() calls this code with current sort and direction params.

 Private Sub LoadOffers(sortExpression As String, direction As String)

        ViewState("sortField") = sortExpression
        If direction = ASCENDING Then
            GridViewSortDirection = SortDirection.Ascending
        Else
            GridViewSortDirection = SortDirection.Descending
        End If

        Dim Conn As SqlConnection
        Dim Cmd As SqlCommand
        Dim dAdapter As New SqlDataAdapter
        Dim dsGifts As New DataSet

        Conn = New SqlClient.SqlConnection(dbConn())
        Cmd = New SqlCommand("sp_Gift_Offers", Conn)
        Cmd.CommandType = CommandType.StoredProcedure
        Conn.Open()
        Cmd.ExecuteNonQuery()
        Conn.Close()
        dAdapter.SelectCommand = Cmd
        dAdapter.Fill(dsGifts, "Gifts")

        dsGifts.Tables("Gifts").DefaultView.Sort = sortExpression & direction

        gv_Gifts.DataSource = dsGifts.Tables("Gifts")
        gv_Gifts.DataBind()

        'LoadOffers
    End Sub

Open in new window

SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland 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
ASKER CERTIFIED SOLUTION
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 rcl58

ASKER

Thanks for the help CodeCruiser.