Datagridview reselect a previosly selected row after refresh()
Question is how to have it so that the last selected row is reselected again after the refresh... something
string currentlySelected = VersionsGridView.SelectedRows[0].Cells[1].Value.ToString());
VersionsGridView.DataSource = Versions.CreateSource();
VersionsGridView.EndEdit();
VersionsGridView.Refresh();
the RowIndex for the "currentlySelected" should be the same as the datasource file contents don't change, but incase it did how to have it search for the 'currentlySelected ' value and reselect it again if it finds it in the rows?
Thanks
Re: Datagridview reselect a previosly selected row after refresh()
Save the value of the primary key (e.g. the row id) before refreshing. After the refresh, find the row with the matching id and select it.
Re: Datagridview reselect a previosly selected row after refresh()
if there are too many rows to search using primary key value, you can store the row index and simply select it back, if it would not have changed immediately after Refresh (it would change in case of a deleted row or table changed).
Doing a refresh itself is costly, so a primary value check would be the best way to go as the mod Arjay has said.
--
BossMode