I found this post looking for the same solution; selecting the last inserted item from a formview/detailsview on a paged gridview.
I was not successful in finding a working example out there, so I thought I'd post my solution for what it's worth. It's working correctly. I welcome any improvements/suggestions from more experienced members.
Problem: Using the GridView.SelectedIndex method to dynamically select a row will not work if the sought after row is in a different page of the GridView.
Solution: Iterate through each page of the GridView until we find the row and then use SelectIndex to focus the user on that row.
Assumptions: As noted in the post above, this assumes we already have the SelectedIndex value we want to select. In this case, a recently inserted identity value added by way of a DetailsView wired to a GridView. We're also assuming the page sizes and total records for the GridView are of a manageable number; if the result set is huge than I wouldn't use this solution. I would instead default new entries to land on the last page of the GridView and use the SelectedIndex on that last page, or limit the results to contain only the newly added row, or a small subset of the data that includes the row.
The following example is in VB. It uses the GridView.DataBound event and iterates through the GridView rows collection on every page looking for the SelectedIndex that matches the DataKey we are seeking. If found, the SelectedIndex is set and the process exits. If value is never found, the GridView is set back to its first page before exiting.
Protected Sub gvwGalleryAdmin_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwGalleryAdmin.DataBound
'_newGalleryID will have a value only when the DetailsView has just completed an insert
'if there is no value, we don't want to do anything
If _newGalleryID > 0 Then
'page through grid until we find the row to select
Dim isSelected As Boolean = False
'keep trying until we find a match or hit the last page/row of GridView
Do While isSelected = False
'use SelectGridViewRow method to search for match
If SelectGridviewRow(gvwGalleryAdmin,CStr(_newGalleryID)) = True Then
'we found our row, reset _newGalleryID
_newGalleryID = 0
'get out
Exit Sub
Else
'item not found on this page, advance to next or exit if last page
If gvwGalleryAdmin.PageIndex < gvwGalleryAdmin.PageCount - 1 Then
'advance to next page
gvwGalleryAdmin.PageIndex = gvwGalleryAdmin.PageIndex + 1
'we exit here because the page change will cause the DataBound event to fire again
'and the next page will be searched
Exit Sub
Else
'did not find row to select in every page, set back to 1st page and exit
gvwGalleryAdmin.PageIndex = 0
Exit Do
End If
End If
Loop
End If
End Sub
Private Function SelectGridviewRow(ByRef thisGridview As GridView, ByVal dataKeyValue As String) As Boolean
'iterate through thisGridview looking for dataKeyValue, return True if found
Dim hasItem As Boolean = False
With thisGridview
For i As Integer = 0 to .Rows.Count - 1
'search the DataKey for our value
If .DataKeys(i).Value = dataKeyValue Then
'we found a match, set the SelectedIndex, return True and get out
.SelectedIndex = i
hasItem = True
Exit For
End If
Next
End With
'if match was not found, hasItem is False
Return hasItem
End Function