DataGridView flickers ONLY when the rows fits roughly one page

让人想犯罪 __ 提交于 2020-06-16 17:27:29

问题


I have a grid view that is as basic as it can gets. It displays a one-off search result, and does not updates its datasource unless the search button is click again, so I don't think data update is causing the issue.

For example, my search returns 30 rows of results, and my full-screen data grid could fit 40 rows without scrolling, there are no flickering. Then I started to slowly reduce the height of the window. Once the grid view height is slightly less than my 30 rows' height it starts to flicker. But if I further decrease the height of the window so that the grid view could only shows 20 rows, a scrollbar shows up as expected and everything is fine again. Even scrolling up or down doesn't flicker at all.

Although I don't think it is performance related, I've tried things like suspend layout and double buffering which doesn't help as expected.

I think it is something related to auto row height calculation when it is near its boundary condition, but I have no idea how to solve it. Below are the auto row size settings that might/might not affect it.

dgv.DataSource = datatable;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgv.Sort(dgv.Columns["Creation Date"], ListSortDirection.Ascending);
dgv.Columns["Creation Date"].SortMode = DataGridViewColumnSortMode.Automatic;
dgv.Columns["Name"].SortMode = DataGridViewColumnSortMode.Automatic;
dgv.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dgv.Columns["Name"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgv.Columns["Receipt No."].SortMode = DataGridViewColumnSortMode.Automatic;
dgv.Columns["Receipt No."].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dgv.Columns["Receipt No."].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgv.Columns["Remark"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgv.Columns["Remark"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

This is my double buffering class just in case I messed up

public static class BufferedGridView
{
    public static void DoubleBuffered(this DataGridView dgv, bool setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, null);
    }
}

This is where I used it, in the Form that contains the grid view;

public ManagementPage()
{
    InitializeComponent();

    dgv.DoubleBuffered(true);
    .......
}

回答1:


I had this problem and after some analysis concluded it's a bug in .NET that happens in the edge case of when a DataGridView is roughly the same height as the rows it needs to contain, because of an endless loop of adding a vertical scrollbar, adjusting row heights, then removing the scrollbar etc.

I've posted the solution I came up with here: DataGridView - apparent bug in .NET causing continuous jitter on the grid view when number of rows is roughly the same as the size of the view



来源:https://stackoverflow.com/questions/53735773/datagridview-flickers-only-when-the-rows-fits-roughly-one-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!