Datagridview FirstDisplayedScrollingRowIndex在网格底部不起作用

本文关键字:底部 不起作用 网格 FirstDisplayedScrollingRowIndex Datagridview | 更新日期: 2023-09-27 18:28:33

为了滚动数据网格,我使用以下代码:

dataGridView1.FirstDisplayedScrollingRowIndex = currentRowIndexInGridView;
dataGridView1.Update();

这对于不在网格底部的行来说很好。如果我将它用于最下面的行,那么当我在调试过程中检查它时,setter不会将它设置为我想要的值。例如,我将FirstDisplayedScrollingRowIndex设置为103,但在分配后,FirstDisplayed ScrollingrowIndex的值为90,因此所需的行不可见。从某个点开始,它停止滚动,我看不到最后5行。如果我添加新行并将其设置为显示,它会滚动一行,但我不会再看到最后5行。

我认为这与我的一些行有不同的高度以及DisplayedRowCount的一些内部估计失败有关???

有没有办法检测这种情况,然后强制滚动到数据网格的底部?

编辑:

FirstDisplayedScrollingRowIndex设置器的重要部分在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变量时似乎出现了错误。

Datagridview FirstDisplayedScrollingRowIndex在网格底部不起作用

每当添加新行时调用以下方法

    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

二传手有毛病。

我也有类似的问题,但我找到了解决方法。问题在于DataGridView需要窗口窗体刷新,只有在刷新之后才能将FirstDisplayedScrollingRowIndex设置为新索引。我做了什么?

Timer refreshTimer = new Timer();
public void RefreshLog()
{
    dataGridViewVesselLog.DataSource = Log.Items;
    dataGridViewVesselLog.Update();
    dataGridViewVesselLog.Refresh();
    refreshTimer.Interval = 100;
    refreshTimer.Tick += (s, e) =>
    {
        if (dataGridViewVesselLog.Rows.Count > 0)
        {
            foreach (DataGridViewRow r in dataGridViewVesselLog.SelectedRows) 
                r.Selected = false;
            dataGridViewVesselLog.Rows[dataGridViewVesselLog.Rows.Count - 1].Selected = true;
            dataGridViewVesselLog.FirstDisplayedScrollingRowIndex = (int)(dataGridViewVesselLog.Rows.Count - 1);
        }
        refreshTimer.Stop();
    }
    refreshTimer.Start();
}