1

I'm using a WinForms DataGridView to display varying amounts of data. When using larger amounts I noticed that AutoSizing all cells results in inacceptable delays until the DataGridView is responsive(from a couple of seconds up to several minutes). Setting the DataGridViewAutoSizeRowsMode property to DisplayedCells rather than AllCells fixed that issue(3 minutes -> 5 seconds).

However now I'm experiencing problems automatically scrolling to the bottom of the DGV's contents. I'm trying to achieve that by setting dgv.FirstDisplayedScrollingRowIndex = dgv.RowCount - 1; which worked just fine before, but now it merely scrolls to one of the last rows and I have to manually click the scroll down button a couple of times until it actually displays the last row. Dragging the scroll bar to the bottom feels very wonky and takes a couple of drags to get to the last row.

I read somewhere else about setting the dgvAnalysis.CurrentCell to a cell in the desired row and also tried the following in the DGV's scroll event handler

private void dgv_Scroll(object sender, ScrollEventArgs e)
{
        if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
        {
            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
        }
}

but neither seems to help. It seems that dgv.FirstDisplayedScrollingRowIndex is not set to the value I give for whatever reason.

Does anybody have an idea how to fix that or what I might've done wrong there?

1 Answer 1

1

Instead of DataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells use the DataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.DisplayedCells) method.

If the DataGridView content doesn't change after loading, then setting the AutoSizeRowsMode property is an overhead. In the form Load event or once the data binding is complete, call the DataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells) and the resizing will be done only once.

1
  • Thanks, but that didn't help me fix my problem. The resizing is basically fine as it is, it's just that it does not scroll to the bottom, since 'FirstDisplayedScrollingRowIndex' is not set to RowCount but a number lower than that.
    – Max M.W.
    Feb 19, 2014 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.