Results 1 to 8 of 8

Thread: Keep scrolling position on DataGridView after update

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Keep scrolling position on DataGridView after update

    I have a DataGridView on a form which has a refresh button on it for re-reading the data in the DataGridView. I would like for the scroll position of the DataGridView to be returned to where it was before re-reading the data rather than positioning the scrolling to the top left. I tried the following code and it is an improvement over setting the scroll to the top left but it still causes some noticeable shift in the DGV's position:

    Code:
    Dim ptCurrentCell As Point
    ptCurrentCell = Me.ReportDataGridView.CurrentCellAddress
    ReportDataGridView.DataSource = dt
    ReportDataGridView.CurrentCell = Me.ReportDataGridView.Rows(ptCurrentCell.Y).Cells(ptCurrentCell.X)
    I also tried:

    Code:
    Dim iHorScroll As Integer
    Dim iVerScroll As Integer
    iHorScroll = ReportDataGridView.FirstDisplayedScrollingRowIndex
    iVerScroll = ReportDataGridView.FirstDisplayedScrollingColumnIndex
    ReportDataGridView.DataSource = dt
    ReportDataGridView.FirstDisplayedScrollingColumnIndex = iHorScroll
    ReportDataGridView.FirstDisplayedScrollingRowIndex = iVerScroll
    Again, a noticeable shift. Is there any way I can get it to reset if not exactly then close to the pre-refeshed position?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Keep scrolling position on DataGridView after update

    If by noticeable shift you mean that there is an observable clearing/populating/scrolling back to the pre-refreshed position going on (presumably very quickly, but still undesirable), then perhaps the below links would help:

    https://stackoverflow.com/questions/...idview-request

    https://stackoverflow.com/questions/...forms-controls

    Note that it is C# Code.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Keep scrolling position on DataGridView after update

    I don't think so. By noticeable shift I mean the rescroll's positioning is not very accurate. So once performed it appears as if the data have shifted a couple of inches. What I want is for it to rescroll to the exact same spot as before, or as close as possible.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Keep scrolling position on DataGridView after update

    If the data is not the same then the scrolling position won't be the same. When you set the data source, all existing rows are removed, hence there is no scrolling at all, and then new rows are added, hence the scrolling position is the top, left of the data. You can then scroll such that the same row and column are at the top and left respectively, but that says nothing about what comes after that and won't even be possible if there isn't enough data to scroll that far.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Keep scrolling position on DataGridView after update

    For my test I display the dgv, change one field in one record (via a popup screen, the dgv itself is read-only), and click the refresh button. The data are the same before the refresh and after except for the one field. The same number of records. The same sort sequence of the records . I have confirmed this. Yes of course

    Code:
    ReportDataGridView.DataSource = dt
    will set the scroll to the top left. That's not the problem because

    Code:
    ReportDataGridView.CurrentCell = Me.ReportDataGridView.Rows(ptCurrentCell.Y).Cells(ptCurrentCell.X)
    will set the scroll where I want it. It just doesn't no a very good job. It's aim is off somewhat. Any suggestions?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Keep scrolling position on DataGridView after update

    That second code snippet just ensures that the current cell is within the viewable area of the grid. It doesn't care whether it's in the same position as before. What happened to getting and setting the FirstDisplayedScrollingColumnIndex and FirstDisplayedScrollingRowIndex properties? I would think that they would get you the closest, although you'd might have to get and set the CurrentCell too if you want focus to remain the same as well as scrolling. If the number of rows change then they may interfere with each other.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Keep scrolling position on DataGridView after update

    I found something else. The following works perfectly for holding the left-right position:

    Code:
    Dim iHorScroll As Integer
    iHorScroll = ReportDataGridView.HorizontalScrollingOffset
    ReportDataGridView.DataSource = dt
    ReportDataGridView.HorizontalScrollingOffset = iHorScroll
    However, if I add the vertical scrolling offset I get an error saying the vertical scrolling is read only. So close yet so far.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Keep scrolling position on DataGridView after update

    Solution:

    Code:
    Dim intRowindex As Integer
    Dim iHorScroll As Integer
    iHorScroll = Me.ReportDataGridView.HorizontalScrollingOffset
    intRowindex = Me.ReportDataGridView.FirstDisplayedScrollingRowIndex
    Me.ReportDataGridView.DataSource = dt
    Me.ReportDataGridView.HorizontalScrollingOffset = iHorScroll
    Me.ReportDataGridView.FirstDisplayedScrollingRowIndex = intRowindex

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width