I need to force the DataGridView
to show the selected row
.
In short, I have a textbox
that changes the DGV
select
Doing something like this:
dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
will only work if the first column is visible. If it is hidden, you'll get an exception. This is safer:
var column = dataGridView1.CurrentCell != null ?
dataGridView1.CurrentCell.ColumnIndex :
dataGridView1.FirstDisplayedScrollingColumnIndex;
dataGridView1.CurrentCell = dataGridView1.Rows[iNextHighlight].Cells[column];
This will reset the selection without scrolling if the target row is already on screen. It also preserves the current column choice which can matter in cases where you've allowed inline editing.