DataGridView vertical scrollbar not updating properly (Forms bug?)

后端 未结 14 1976
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 23:49

I\'ve encountered a bug (I assume) in .NET 3.5. When adding rows to a DataGridView using Rows.Add(), while the DGV is disabled, the vertical scrollbar doesn\'t update proper

14条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 23:56

    For me the problem was that I put my datagrid in a TabPage which was not displayed during data generation time so the srolling was messed up.

    I found a was to make a correct update just by auto diable/enable at each visible change :

    public MyForm()
    {
        InitializeComponent();
    
        // Automatic scroll to bottom (it might not work if you try to do it without this event)
        datagrid.RowsAdded += (sender, e) =>
        {
            datagrid.FirstDisplayedScrollingRowIndex = datagrid.RowCount - 1;
        };
    
        // WinForms bug fix: scrollbar update
        datagrid.VisibleChanged += (sender, e) =>
        {
            Enabled = false;
            Enabled = true;
        };
    }
    

提交回复
热议问题