Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagridview FirstDisplayedScrollingRowIndex not working at the bottom of the grid

For scrolling the datagrid, I use following code:

dataGridView1.FirstDisplayedScrollingRowIndex = currentRowIndexInGridView;
dataGridView1.Update();

That works fine for rows not at the bottom of the grid. If I use it for bottom rows, the setter doesnt set it to the value I wanted, when I inspect it during debugging. E.g. I am setting FirstDisplayedScrollingRowIndex= 103, but after the assignment FirstDisplayedScrollingRowIndex has the value 90 and hence the desired row is not visible. From a certain point it stops scrolling and I cant see the last 5 rows. If I am adding new rows and setting them to be displayed, it scrolls by one, but I dont see the last 5 rows again.

I think it has something to do with the fact, the some of my rows have different height and some internal estiment of DisplayedRowCount fails ???

Is there a way of detecting this situation and then forcing scrolling to the bottom of the datagrid?

EDIT:

The important part of the FirstDisplayedScrollingRowIndex setter looks like this in the 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 in computing the rows variable.

like image 448
Tomas Grosup Avatar asked Oct 26 '25 02:10

Tomas Grosup


1 Answers

Call following method whenever a new row is added

    private void Autoscroll()
    {            

        if (dgv.FirstDisplayedScrollingRowIndex + dgv.DisplayedRowCount(false) < dgv.Rows.Count)
        {
            dgv.FirstDisplayedScrollingRowIndex += dgv.DisplayedRowCount(false);
        }
        else
        {
            dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
        }

    }
like image 106
Amritpal Singh Avatar answered Oct 28 '25 15:10

Amritpal Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!