Hello Everyone,
I've been grappling with a problem involving corruption of the vertical scrollbar display when I change the DataSource binding. I have a bound DataGridView on a form that I populate using the DataSource property from a simple ArrayList. This grid ArrayList is populated from another ArrayList which contains the entire dataset. As the user scrolls through the grid, the grid ArrayList is re-populated with the next set of 500 elements from the master ArrayList. AutoGenerateColumns is set true so that the columns are labelled properly and both horizontal and vertical scrollbars are enabled. Because my dataset is much bigger than what I want to populate the grid with at once, I've tied into the Scroll event with a handler. I populate the grid with 500 rows at a time. Here is a snippet from my scroll event handler:
Code Snippet
private void inspectionGrid_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
if (e.NewValue > e.OldValue) // Scroll down
{
if (((inspectionGrid.FirstDisplayedScrollingRowIndex + inspectionGrid.DisplayedRowCount(true)) > (m_gridList.Count - 1)) &&
(((m_displayedDataPageInGrid * m_numRowsInGridView) + inspectionGrid.FirstDisplayedScrollingRowIndex + inspectionGrid.DisplayedRowCount(true)) <= m_tableObjectList.Count))
{
m_displayedDataPageInGrid++; // Page index into the master row list
int startIndexOfMasterRowList = (m_displayedDataPageInGrid * m_numRowsInGridView) - inspectionGrid.DisplayedRowCount(true);
int numNewRowsToPopulateInGridView;
if (m_displayedDataPageInGrid > 0)
{
numNewRowsToPopulateInGridView = m_numRowsInGridView + inspectionGrid.DisplayedRowCount(true);
}
else
{
numNewRowsToPopulateInGridView = m_numRowsInGridView;
}
m_gridList = new ArrayList();
for (int i = 0; i < numNewRowsToPopulateInGridView; i++)
{
// Have we hit the end of the master list?
if ((startIndexOfMasterRowList + i) > (m_tableObjectList.Count - 1))
{
break;
}
m_gridList.Add(m_tableObjectList[startIndexOfMasterRowList + i]);
}
inspectionGrid.DataSource = m_gridList;
inspectionGrid.FirstDisplayedScrollingRowIndex = 0;
inspectionGrid.CurrentCell = inspectionGrid.FirstDisplayedCell;
}
}
The handler determines if the 500th element has been scrolled to at the bottom of the grid. If so, then the m_gridList is re-populated with the next 500 rows from the m_tableObjectList and the DataSourcere-assigned to the updated m_gridList. I presume this should change the data bindings. Ifthe scroll is performed with the down arrow button on the scrollbar, after the grid is updated, the vertical scrollbar display is corrupted and is no longer functional (i.e., only partially drawn and clicking on it does nothing). The horizontal scrollbar is unaffected.
If I scroll instead with "Page Down" causing large scroll increments, however,the scrollbar is drawn correctly. Corruption happens only on a single scroll down, not page down. Also, if I close the form that the DataGridView resides on and re-open it, the vertical scrollbar is displayed correctly.
I've tried all sorts of things, like (1) hiding the grid before changing the DataSource and re-displaying it, (2) calling Refresh(), (3) calling Invalidate() followed by Update(), (4) calling PerformLayout(), etc. Nothing fixes the problem. I've also directly addressed the vertical scrollbar control and tried refreshing it, making sure its Minimum and Maximum values are set properly. Again, no dice.
I've seen some references elsewhere in the forum mentioning this type of vertical scrollbar corruption - are these known issues with DataGridView?
I really don't want to have to implement my own VScrollBar and tie its events into the DataGridView... 
Thanks in advance.