如何调整数据网格视图的垂直滚动位置以在更改其高度后显示整个最后一行

本文关键字:高度 显示 一行 最后 位置 数据 数据网 调整 何调整 网格 视图 | 更新日期: 2023-09-27 18:36:22

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

这是我的代码:

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();
}

如何调整数据网格视图的垂直滚动位置以在更改其高度后显示整个最后一行

我想

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

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