title: Populating a DataGridView on-the-fly (VB.NET)
body: <p>I have a DataGridView which reads data from a custom (CSLA) list object. The dataset might have 100,000 records, and obviously I don't want to show them all at once because, for one, it would take ages to load.</p>
  
  <p>I think it would be nice if I could read the first (for instance) 20 records, and make it so upon scrolling to the end of the list, it reads the next 20 and adds them to the DataGridView.</p>
  
  <p>I have experimented with various ways of doing this, mostly using a custom class inheriting from DataGridView to capture scroll events and raising an event which adds new records. I will include the code below:</p>
  
  <pre><code>Public Class TestDGV
  Inherits DataGridView
  
  
  Public Sub New()
  
      AddHandler Me.VerticalScrollBar.ValueChanged, AddressOf vsScrollEvent
      AddHandler Me.RowsAdded, AddressOf vsRowsAddedEvent
  End Sub
  
  Private Sub vsScrollEvent(ByVal sender As Object, _
                            ByVal e As EventArgs)
      With DirectCast(sender, ScrollBar)
  
          If .Value &gt;= (.Maximum - .LargeChange) Then
              RaiseEvent ScrollToEnd()
          End If
      End With
  
  
  
  
  End Sub
  
  
  Private Sub vsRowsAddedEvent(ByVal sender As Object, ByVal e As EventArgs)
      ScrollbarOn()
  End Sub
  
  Public Event ScrollToEnd()
  
  Public Sub ScrollbarOff()
      Me.VerticalScrollBar.Enabled = False
  End Sub
  
  Public Sub ScrollbarOn()
      Me.VerticalScrollBar.Enabled = True
  End Sub
  
  End Class
  </code></pre>
  
  <p>Though this (sort of) works, it can be buggy. The biggest problem was that if you used the mouse to scroll the DataGrid, it would get stuck in a loop as it process the scrollbar's ValueChanged event after the data was added. That's why I added ScrollbarOff and ScrollbarOn - I call them before and after getting new records, which disables the scrollbar temporarily.</p>
  
  <p>The problem with <strong>that</strong> is that after the scrollbar is re-enabled, it doesn't keep track of the current mouse state, so if you hold down the 'Down' button with the mouse (or click on part of the scrollbar) it stops scrolling after it has added the new records, and you have to click it again.</p>
  
  <p>Also, it doesn't seem a particularly elegant way of doing things.</p>
  
  <p>Has anyone ever done this before, and how did you achieve it?</p>
  
  <p>Cheers.</p>