Store and restore the currently selected cell on a datagridview

The standard .NET datagridview has several properties for active selections, but storing and restoring the current location of the user in the grid can be surprisingly un-intuitive.

While you can use the SelectedRows propertie to query active selections, re-activating these selections in the rows[] collection, this will not restore focus to the row that was active before your event.

The most effective way to archive this is querying the active cell before your event, and restoring the active cell after your event, as shown below.

int rowIndex = DataGridView1.CurrentCell.RowIndex;
int colIndex = DataGridView1.CurrentCell.ColumnIndex;
// call to your event code
DataGridView1.CurrentCell = DataGridView1[colIndex, rowIndex];