Datagridview FirstDisplayedScrollingRowIndex不在网格底部工作
为了滚动数据网格,我使用以下代码:
dataGridView1.FirstDisplayedScrollingRowIndex = currentRowIndexInGridView; dataGridView1.Update();
这适用于不在网格底部的行。 如果我将它用于底行,那么当我在调试期间检查它时,setter不会将它设置为我想要的值。 例如,我设置了FirstDisplayedScrollingRowIndex = 103,但在赋值后,FirstDisplayedScrollingRowIndex的值为90,因此所需的行不可见。 从某一点开始它停止滚动,我看不到最后5行。 如果我要添加新行并将它们设置为显示,它会滚动一个,但我再也看不到最后5行了。
我认为它与事实有关,我的一些行有不同的高度和一些内部的DisplayedRowCount情绪失败???
有没有办法检测这种情况,然后强制滚动到数据网格的底部?
编辑:
FirstDisplayedScrollingRowIndex setter的重要部分在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); }
计算rows变量时似乎有错误。
每当添加新行时,请调用以下方法
private void Autoscroll() { if (dgv.FirstDisplayedScrollingRowIndex + dgv.DisplayedRowCount(false) < dgv.Rows.Count) { dgv.FirstDisplayedScrollingRowIndex += dgv.DisplayedRowCount(false); } else { dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1; } }
我必须强制所有行具有相同的宽度,否则
FirstDisplayedScrollingRowIndex
setter是越野车。