views:

313

answers:

1

I am working (fixing bugs) on a project which was written in VS 2005. There is one DataGridView control on a form. When it is first time loaded, the control's data grid is populated with rows of data from a collection manually or in codes. Actually, there is method PopulateDataGrid() do the job.

There is also another control on the form. When control is changed, the data grid will be cleared first and then rows are repopulated again through PopulateDataGrid(). The problem is that when the grid is refreshed, the vertical scroll bar does not get reset correctly. I thought it should be. Since the scroll bar is not reset, when I tried to click on grid and move down, I got exception: {"Value of '222' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.\r\nParameter name: Value"}:

 at System.Windows.Forms.ScrollBar.set_Value(Int32 value)
 at System.Windows.Forms.DataGridView.ScrollRows(Int32 rowCount, Int32 deltaY, ScrollEventType scrollEventType)
 at System.Windows.Forms.DataGridView.ScrollRowsByCount(Int32 rows, ScrollEventType scrollEventType)
 at System.Windows.Forms.DataGridView.ScrollRowIntoView(Int32 columnIndex, Int32 rowIndex, Boolean committed, Boolean forCurrentCellChange)
 at System.Windows.Forms.DataGridView.ScrollIntoView(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
 at System.Windows.Forms.DataGridView.ProcessDownKeyInternal(Keys keyData, Boolean& moved)
 at System.Windows.Forms.DataGridView.ProcessDataGridViewKey(KeyEventArgs e)
 at System.Windows.Forms.DataGridView.OnKeyDown(KeyEventArgs e)
 at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
 ...

All the settings for grid control are default values. For example, the ScrollBars is Both. The following is the only related place to set row auto size property:

poDataGridView.AutoSizeRowsMode =
            DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

I am not sure if there is any property I have to set in designer?

+1  A: 

I think I got the issue resolved. I have to set control's scrollbars to none before the refresh and reset back to both in my refresh method call:

 private void PopulateDataGrid() {
    dataGrid.Rows.Clear();
    dataGrid.ScrollBars = ScrollBars.None;
    // continue to get new data and populate cells....
    dataGrid.ScrollBars = ScrollBars.Both;
 }
David.Chu.ca