Problems setting vertical scrollbar value in a datagrid (old one, not the better DataGridView).

Posted by user365581 on Stack Overflow See other posts from Stack Overflow or by user365581
Published on 2010-06-13T08:17:45Z Indexed on 2010/06/13 8:22 UTC
Read the original article Hit count: 151

Filed under:
|
|

I need to save the selected row and the vertical scrollBar's position after a refresh. This is how I do it:

int currRow = myGrid.CurrentRowIndex;
int vScrollPos = ((ScrollBar)myGrid.Controls[1]).Value
// some code that refreshes the data among other things
myGrid.CurrentRowIndex = currRow; // this sets the property
myGrid.Select(currRow); // this selects in UI (both commands required)
((ScrollBar)myGrid.Controls[1]).Value = vScrollPos;

Here's my problem: The grid always jumps to a place where the selected row is at the bottom. setting the current row makes it happen - similar to EnsureVisible of newer grid implementations. But after that there's the vScrollBar repositioning - and it just doesn't work right. In debug I see that the scroll bar value gets updated. In the UI, if I hit the down/up arrow on the scrollbar it suddenly jumps to the right place - But if I don't click anything the grid is just in the wrong position. I tried refreshing the grid/scroll bar to force a redraw, but it doesn't help. The actual grid position is just not in sync with the vertical ScrollBar's value. Any ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about datagrid