Hi,
I have a DataGridView that is data-bound to a List(Of SomeClass). Some actions the user can take may require the entire grid to 'refresh' (meaning: update its data). In that case, I simply load the new List(Of SomeClass) and bind it to the grid again by setting the DataSource property.
The problem now is that the selected rows get lost. This isn't much of a problem, but if the grid contains many items (which it probably will) then it will automatically scroll back up to the first item and the user may lose the item he was looking at.
How can I keep (re-set) the previously selected rows, provided they are still available?
One of the problems I can see is that the previously selected rows may contain one or a few rows that no longer exist.
I tried doing this, which "in theory" does what I want, but doesn't work for a very simple reason:
It stores the previously selected rows, loads the new data, then loops through all rows and selects them if the current row was in the previously selected rows collection.vb.net Code:
Dim selectedRows = grid.SelectedRows grid.DataSource = Me.GetNewData() For Each row As DataGridViewRow In grid.Rows row.Selected = selectedRows.Contains(row) Next
Obviously this doesn't work because (presumably) new DataGridViewRow objects are being created when I set the DataSource, so even if the rows may have the same data, they are no longer the same object, and no rows are selected after I run this code.
I can't rely on the indeces of the rows either, because as said before, some rows may no longer exist...
How is this usually done?




Reply With Quote