如何调整DataGridView的垂直滚动位置以在更改其高度后显示整个最后一行?

时间:2015-01-06 16:12:54

标签: c# datagridview scroll

我有一个DataGridView,通常每行显示一行数据。但是当用户进入单元格时,我会增加最小高度以使编辑更容易。但是,当网格中的最后一行发生这种情况时,垂直滚动位置不会被调整,只有行的顶部可见,这通常会使其显示为空白。将FirstDisplayedScrollingRowIndex设置为该行的索引不会解决此问题。

这是我的代码:

protected override void OnCellEnter(DataGridViewCellEventArgs e)
{
    base.OnCellEnter(e);

    // Looks weird, but this solves an annoying problem where the wait cursor gets stuck
    // on randomly after displaying one of the segmentation dialogs.
    // See http://stackoverflow.com/questions/3008958/datagridview-retains-waitcursor-when-updated-from-thread/13808474#13808474
    Cursor = Cursors.Default;

    EditMode = (GetIgnoreStateForRow(CurrentCellAddress.Y)) ? DataGridViewEditMode.EditProgrammatically : DataGridViewEditMode.EditOnEnter;
    if (e.ColumnIndex != 0 || CurrentCellAddress.Y < 0 || (!Focused && (EditingControl == null || !EditingControl.Focused)))
    {
        var minHeight = RowTemplate.Height * 3;
        if (CurrentRow != null && CurrentRow.Height < minHeight)
            CurrentRow.MinimumHeight = minHeight;
        return;
    }
    SchedulePlaybackForCell();
}

1 个答案:

答案 0 :(得分:0)

我想我找到了答案。它不一定像我希望的那样优雅,因为它需要覆盖另一种方法,从而将“修复”与“问题”分开。但它确实有效:

    protected override void OnRowHeightChanged(DataGridViewRowEventArgs e)
    {
        base.OnRowHeightChanged(e);
        if (CurrentRow == e.Row && e.Row.Index == RowCount - 1)
            FirstDisplayedScrollingRowIndex = e.Row.Index;
    }