如何调整 DataGridView 的垂直滚动位置以在更改其高度后显示整个最后一行?
How to adjust vertical scroll position of DataGridView to show entire last row after changing its height?
我有一个 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
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();
}
我想我找到了答案。它不一定像我希望的那样优雅,因为它需要覆盖另一个方法,从而将 "fix" 与 "problem" 分开。但它似乎确实有效:
protected override void OnRowHeightChanged(DataGridViewRowEventArgs e)
{
base.OnRowHeightChanged(e);
if (CurrentRow == e.Row && e.Row.Index == RowCount - 1)
FirstDisplayedScrollingRowIndex = e.Row.Index;
}
我有一个 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
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();
}
我想我找到了答案。它不一定像我希望的那样优雅,因为它需要覆盖另一个方法,从而将 "fix" 与 "problem" 分开。但它似乎确实有效:
protected override void OnRowHeightChanged(DataGridViewRowEventArgs e)
{
base.OnRowHeightChanged(e);
if (CurrentRow == e.Row && e.Row.Index == RowCount - 1)
FirstDisplayedScrollingRowIndex = e.Row.Index;
}