Weird scrolling problem with DataGridView using virtual mode and just-in-time loading.

Ok, so I have a record navigation box (datagridview) which displays search results, potentially exceeding 100k records. The datagridview will obviously lag the program to the point of unusability, so I had to use virtual mode to load only the records necessary for viewing, until the user scrolls, and then more records will be loaded on demand. Here is the Scrolling event handler which triggers the loading of more rows to the DGV control:

 private void ScrollHandler(object sender, ScrollEventArgs e) { if (e.ScrollOrientation == ScrollOrientation.VerticalScroll) { if (e.NewValue >= (dgSearch.RowCount - RowBufferSize)) { dgSearch.RowCount = Math.Min(dgSearch.RowCount + RowBufferSize, dbi.DataSet.Tables[0].Rows.Count); } } } 

For the most part, it does the job: rows are added automatically when the user scrolls past the row which previously marked the end of the DGV's row collection. Here's the problem: if I click and hold in the lower area, below the scroll position bar, so that I'm constantly scrolling down, eventually the code enters into what seems to be a loop that's nearly indefinite. Does anyone have an idea about how I might make use of some kind of stopping condition to prevent the loop?

by Nanopants via /r/csharp

Leave a Reply