I need to force the DataGridView
to show the selected row
.
In short, I have a textbox
that changes the DGV
selection based on what is typed into the textbox
. When this happens, the selection changes to the matching row
.
Unfortunately if the selected row
is out of the view, I have to manually scroll down to find the selection. Does anyone know how to force the DGV
to show the selected row
?
Thanks!
You can set:
Here is the MSDN documentation on this property.
Just put that line after the selecting the row:
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.
I made the next search function it works wel for scrolling selections in display.
}
Consider also this code (uses the from competent_tech suggested way):
visibleColumnIndex - selected cell must be visible