private void RefreshImages() { Image selected = null; if (dataGridView1.SelectedRows.Count == 1) { int index = dataGridView1.SelectedRows[0].Index; selected = images[index]; } var column = dataGridView1.SortedColumn; var order = dataGridView1.SortOrder; images.ResetItems(db.imagesDict.Values); if (selected != null) { int index = images.IndexOf(selected); if (index != -1) { dataGridView1.Rows[index].Selected = true; } } }