David.Chu.ca
David.Chu.ca

Reputation: 38644

DataGridView: Scroll bar does not get refreshed

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?

Upvotes: 2

Views: 9336

Answers (3)

Mr_CRivera
Mr_CRivera

Reputation: 239

You can use

dataGridView.PerformLayout(); 

In order to force refresh of your data grid scroll bar, this usually solves this problem, but if it doesn't work, just make sure that the insertions and deletions of columns in your dataGrid are done while it is active (enabled) and it will refresh by itself correctly.

Upvotes: 3

Steve
Steve

Reputation: 69

I had a similar situation with a DataGrid on one tab and some input controls on another tab. I would update the data using the controls, and save and refresh the data. Going back to the main list tab, the scrollbar would be disabled.

I found that resetting the scrollbars using code in the datarefresh method did not solve the problem.

How I got around it was to set a flag indicating that the data had been refreshed and when the user selected the main list tab (containing the datagrid), this flag would determine whether to manually set the grid sort column and sort direction. This seemed to work.

It seems that the grid has to be active, and then resetting the sort column will cause the scrollbars to update. But what a workaround!

Upvotes: 0

David.Chu.ca
David.Chu.ca

Reputation: 38644

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;
 }

Upvotes: 4

Related Questions