DataGridView: scrollbar not updating
I am working (fixing bugs) on a project that was written in VS 2005. There is one DataGridView control in the form. The first time it is loaded, the control data grid is populated with data series from the collection, either manually or in codes. In fact, the PopulateDataGrid () method does the job.
There is another control on the form as well. When the control is changed, the data grid will be cleared first and then the rows will be populated again via PopulateDataGrid (). The problem is when the grid is refreshed the vertical scrollbar doesn't get reset correctly. I thought it should be. Since the scrollbar is not reset, when I tried to click on the grid and go down, I got an exception: {"The value" 222 "is not valid for" Value "." The value "must be between" minimum "and 'maximum'. \ R \ nParameter: 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 settings for mesh control are default values. For example, ScrollBars is Both. The following is the only related place to set the auto line size property:
poDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
I'm not sure if there is any property that I should be setting in the designer?
a source to share
I think I solved the problem. I have to set the control scrollbars until the update is refreshed and reset back to both call methods:
private void PopulateDataGrid() {
dataGrid.Rows.Clear();
dataGrid.ScrollBars = ScrollBars.None;
// continue to get new data and populate cells....
dataGrid.ScrollBars = ScrollBars.Both;
}
a source to share
you can use
dataGridView.PerformLayout();
To force the scrollbar of your DataGrid to refresh usually solves this problem, but if it doesn't work, just make sure the inserts and deletions of columns in your DataGrid are done when it is activated (enabled) and it will update itself correctly.
a source to share
I had a similar situation with a DataGrid in one tab and some input controls in another tab. I would update the data with controls and save and update the data. Returning to the main list tab, the scroll bar will be disabled.
I found that resetting the scrollbars using the code in the datarefresh method did not solve the problem.
The way I got around this was to set a flag indicating that the data was updated, and when the user selected the master list tab (containing the datagrid), this flag would determine whether to manually set the grid sort column and sort direction.It seemed to work.
It seems like the grid should be active and then resetting the sort column will update the scrollbar. But what a workaround!
a source to share