Click here to Skip to main content
15,891,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have one gridview and it contains no.of records. if i have scroll down for 50th or any other record then if i refresh the form through a button, i will lose my current positon and automatically comes to 1st record. I want the record at the same position say 50 even after refreshing. Please help me...

Thanks in advance.
Posted

I use the DataGridView's FirstDisplayedScrollingRowIndex Property[^]

Like this: (my DataGridView is named gvMast)

VB
'Variable to save the scrolling index with
    Private intSaveScrollingIndex As Integer = 0

    'When user scrolls the grid, save the index
    Private Sub gvMast_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles gvMast.Scroll
        intSaveScrollingIndex = gvMast.FirstDisplayedScrollingRowIndex
    End Sub

    'When the grid is refreshed, scroll the grid back to the proper place if possible
    Private Sub LoadGrid()
        ' *** Whatever code you have that reloads/refreshed your grid goes here *** '

        'If filters or changes have removed rows to scroll to, or have removed ALL rows, reset the scrolling index to zero
        If gvMast.Rows.Count <= intSaveScrollingIndex _
        OrElse intSaveScrollingIndex = -1 Then
            intSaveScrollingIndex = 0
        End If

        'Only set scrolling index when there are rows to display
        If gvMast.Rows.Count > 0 Then
            gvMast.FirstDisplayedScrollingRowIndex = intSaveScrollingIndex
        End If
    End Sub
 
Share this answer
 
Comments
aktharulameen 17-Dec-12 1:31am    
Hi Kschuler,

The above gridview's property FirstDisplayedScrollingRowIndex is worked fine for me.

Thank you so much to give me the best solution. Once again thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900