I apologize in advance if my question is hard to understand because of my broken English.
In my Windows Form Application in C#, I have a DataGridView
control that keeps showing the most recent table data. With a Timer
control, it automatically refreshes the table in the DataGridView
.
Since there are more columns than the DataGridView
can show at once, there is a horizontal scrollbar
.
I am also using FirstDisplayedScrollingColumnIndex
to get the current scroll position. This way, when the user is browsing the Table with the scrollbar
and the table refresh occurs, the scrollbar
can stay where it was before the refresh.
However, there is one problem. Since FirstDisplayedScrollingColumnIndex
can only store an integer value, when the table's leftmost column within the view is not fully shown, it snaps back upon table refresh, so that the leftmost column within the view becomes fully shown. This becomes an issue when the user is browsing the rightmost column with the scrollbar
because upon refresh, it snaps back to display the leftmost column, instead of the rightmost one, that is, the scrollbar
does not stay at the rightmost position.
Here is the picture. This is what it should keep showing upon table refresh:
As you can see, Sex column is fully shown but the leftmost column is halfway shown. When the table refresh occurs, it snaps one column back to show the leftmost column within the view. This prevents the user from viewing the Sex
column's data when the refresh occurs. (Picture below) I think this is because FirstDisplayedScrollingColumnIndex
stores an integer value, the column position.
private int scrollPositionColumn;
private void Timer_Tick(object sender, EventArgs e){
//Before the update, save the current scrollbar position
if(MyDGV.FirstDisplayedScrollingColumnIndex >= 0){
scrollPositionColumn = MyDGV.FirstDisplayedScrollingColumnIndex;
}
UpdateGridView();
}
private void UpdateGridView(){
/*Some code*/
//Restore the previous position of the scrollbar
MyDGV.FirstDisplayedScrollingColumnIndex = scrollPositionColumn;
/*Some code*/
}
Can someone please give me advice on how to fix this issue?
Here is the simplified code that I have.