Remebering selected rows after a refresh on a .NET DataGridView
Have you ever tried using the .NET DataGridView Control bound to a datasource which is refreshed periodically along with the benefit of being able to select rows? It seems to be a common request - also some of the 3rd party grid controls support this by default.
When attempting to do this it becomes quickly apparent that immediatly after a refresh on the datasource the selected row jumps back to the first row in the grid - quite annoying. I'm not quite sure why there can't be a degree of separation between the datasource and the grid user model - it would make the use of the control much better.
Anyhow enclosed is the work around to resolving this problem
Prior to refreshing your datasource store the index of the selected row for safe keeping and then restore it after the refresh.
iRowIndex = dgData.CurrentRow.Index
dgData.DataSource = myDataSource
dgData.Refresh()
dgData.CurrentCell = dgData.Rows(iRowIndex).Cells(0)
