I have a standard DataGridView within a WinForms application.
The grid does not allow multi select.
I have a situation where I need to reload the grid's data and return the user back to the row they were focused on.
The code below works in terms of having the row required selected, however, the row is not focused.
The focus indicator stays on the first row of the grid.
How do I move focus to this row rather than just selecting it? I don't really even need to select it, I just want to put the user to the row that they were on.
var selectedItem = string.Empty;
if (grd.CurrentRow != null)
selectedItem = ((Item) grd.CurrentRow.DataBoundItem).ItemId;
this.ItemBindingSource.DataSource = null;
get my data
this.ItemBindingSource.DataSource = data;
foreach (DataGridViewRow row in grd.Rows)
{
if (row != null && (string) row.Cells[0].Value == selectedItem)
{
grd.ClearSelection();
row.Selected = true;
break;
}
}