Datagridview FirstDisplayedScrollingRowIndex not working at the bottom of the grid
I use the following code to scroll the datagrid:
dataGridView1.FirstDisplayedScrollingRowIndex = currentRowIndexInGridView; dataGridView1.Update();
This works great for rows and not at the bottom of the grid. If I use it for the bottom lines, the setter doesn't set it to the value I wanted when I check it while debugging. For example. I am setting FirstDisplayedScrollingRowIndex = 103, but after assigning FirstDisplayedScrollingRowIndex is 90 and hence the desired row is not displayed. From a certain point it stops scrolling and I can't see the last 5 lines. If I add new lines and set them to display, it scrolls by one, but I no longer see the last 5 lines.
I think it has something to do with the fact, some of my rows are at different heights, and some of the internal DisplayedRowCount aesthetics are failing.
Is there a way to detect this situation and then force scrolling to the bottom of the datagrid?
EDIT:
The important part of the FirstDisplayedScrollingRowIndex installer looks like this Reflector:
if (value > this.displayedBandsInfo.FirstDisplayedScrollingRow)
{
int rows = this.Rows.GetRowCount(DataGridViewElementStates.Visible, this.displayedBandsInfo.FirstDisplayedScrollingRow, value);
this.ScrollRowsByCount(rows, (rows > 1) ? ScrollEventType.LargeIncrement : ScrollEventType.SmallIncrement);
}
else
{
this.ScrollRowIntoView(-1, value, true, false);
}
There seems to be an error while evaluating the string variable.
source to share
Use the following method when adding a new line
private void Autoscroll()
{
if (dgv.FirstDisplayedScrollingRowIndex + dgv.DisplayedRowCount(false) < dgv.Rows.Count)
{
dgv.FirstDisplayedScrollingRowIndex += dgv.DisplayedRowCount(false);
}
else
{
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
}
}
source to share