Results 1 to 5 of 5

Thread: Autoscrolling to bottom of datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    12

    Autoscrolling to bottom of datagridview

    My application adds rows to a datagridview, but when it adds rows past the bottom of the visible datagridview on the form I want the grid to scroll up so that the last entry is always shown and the user doesn't have to manually scroll to the bottom.

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Autoscrolling to bottom of datagridview

    Code:
    Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
       DataGridView1.FirstDisplayedScrollingRowIndex = e.RowIndex + e.RowCount - 1
    End Sub

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

    Re: Autoscrolling to bottom of datagridview

    The grid can't actually scroll past the last row being at the bottom of the grid so you can simply specify the last row as the first displayed row and the grid will just scroll as far as it can.
    Code:
    Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Autoscrolling to bottom of datagridview

    Quote Originally Posted by jmcilhinney View Post
    The grid can't actually scroll past the last row being at the bottom of the grid so you can simply specify the last row as the first displayed row and the grid will just scroll as far as it can.
    Code:
    Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1
    As the OP has phrased the question, this is probably valid. However it presumes that the added row will be positioned as the last displayed row. If the control is subject to sorting this may not be the case. Using the event with its supplied data will work in all cases.

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

    Re: Autoscrolling to bottom of datagridview

    Quote Originally Posted by TnTinMN View Post
    As the OP has phrased the question, this is probably valid. However it presumes that the added row will be positioned as the last displayed row. If the control is subject to sorting this may not be the case. Using the event with its supplied data will work in all cases.
    Valid point.

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