DataGridView not rendering random cells

放肆的年华 提交于 2019-12-14 03:15:06

问题


I have a datagridview which I am populating from a datasource using dgv.DataSource = table. I then use a cell formatting event to change the color of specific cells in the datagridview depending on a value. The problem that I am facing is that when looking at the data on certain pc's some random cells will appear white with no data in. The code I am using to set the color is below however it is not just the cells which I have set custom colors which appear white:

    private void dgvRaw_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e != null)
        {
            DataGridView dgv = (DataGridView)sender;

            if (dFlags.ContainsKey(dgv.Columns[e.ColumnIndex].Name))
            {
                e.CellStyle.ForeColor = Color.Black;
                // If pass set green else set red 
                if (e.Value != null)
                {
                    if (e.Value.ToString() == "0")
                        e.CellStyle.BackColor = System.Drawing.Color.Green;
                    else
                        e.CellStyle.BackColor = System.Drawing.Color.Red;
                }
                else
                    e.CellStyle.BackColor = System.Drawing.Color.Orange;
            }
        }
    }

I literally have no idea why this is happening or if it is due to the computers not being able to cope with rendering large grid view. Thanks!


回答1:


As it turns out the issue with the datagridview was that double buffering is not enabled by default. I used a custom datagridview class to enable double buffering and since then I have not had any issues, I suspect because there was such a large dataset it was having problems rendering the entire area and on the lower end pcs it was just giving up. I have included the class below

    /// <summary>
    /// Custom datagridview to enable double buffering
    /// </summary>
    public class MyDataGridView : DataGridView
    {
        public MyDataGridView()
        {
            DoubleBuffered = true;
        }
    }

I cannot take credit for this code as I found it on another source on Stack Overflow however it was for a different problem.



来源:https://stackoverflow.com/questions/16675155/datagridview-not-rendering-random-cells

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