3

I am binding my grid as such:

dataGridView1.DataSource = new BindingSource();

dataGridView1.DataSource = tableData; (tableData is an ArrayList of custom objects)

The dataSource is getting updated very often (the file it is reading from gets updated about every 2 ms). So when I am scrolling, the scrollbar will jump to it's original position upon a refresh. I refresh like this:

((CurrencyManager)dataGridView1.BindingContext[tableData]).Refresh(); (this happens once every ~1 second)

How can I scroll without the scroll bar resetting every time the datagridview gets refreshed?

jpints14
  • 1,361
  • 6
  • 15
  • 24

2 Answers2

5

Take a look at this. Although the question is about Winforms DataGrid, the answer is applies to DataGridView. You need to store FirstDisplayedScrollingRowIndex before the reload and restore it after.

Community
  • 1
  • 1
Raj Ranjhan
  • 3,869
  • 2
  • 19
  • 29
  • this seems to work. Given this is a very large data set, the scrolling is not very smooth. I can drag the bar a certain amount, and then it will stop following my cursor, so I have to go grab it again and repeat. Is this simply because it is very big, or because when it refreshes/handles row index, it stops the dragging motion? – jpints14 Mar 06 '12 at 17:16
  • Scrolling speed will depend on the hardware too. You may try [this](http://www.dreamincode.net/forums/topic/188953-c%23-datagridview-doublebuffered-property/). I have not used this so I am not sure if it actually works. – Raj Ranjhan Mar 06 '12 at 18:02
0

I know it's been a while since you posted this question but I just ran into this kind of issue myself. One thing to check is to be sure you are not setting the CurrentCell property when your grid updates

Code such as the following will cause your scroll position to reset. You are telling it to view a particular row, and the first cell in that row.

YourGrid.CurrentCell = YourGrid[0, row];

Hope this helps.

DC

David Christiansen
  • 5,869
  • 2
  • 36
  • 42