0

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!

Yograj Gupta
  • 9,811
  • 3
  • 29
  • 48
manicmonkey21421
  • 113
  • 2
  • 12
  • How many items do you display in this grid ? – Ofiris May 21 '13 at 16:41
  • In order to eliminate any display bug from the equation, try checking the source code : is the data in it ? If yes but it's not displayed : display bug. If not... then it's something else, and very strange – Laurent S. May 21 '13 at 16:42
  • It depends on the query, but probably somewhere on average of 100 columns and 1000 rows. I have the same problem with a smaller data grid view which only has 10 columns and 5-20 rows so I am skeptical that it is to do with the size of the dataset – manicmonkey21421 May 21 '13 at 16:43
  • How about trying to refresh the grid using `dgv.refresh()` ? – Ofiris May 21 '13 at 16:44
  • @Bartdude I know the data is in it because if a cell is clicked on the data will then appear however another white cell will sometimes appear somewhere else, especially if highlighting data. – manicmonkey21421 May 21 '13 at 16:45
  • @Ofiris I think I have tried this in the past to no effect however I will give it a go on one of the pcs where it is an issue tomorrow when I have access. Thanks for the answer though! – manicmonkey21421 May 21 '13 at 16:48
  • from what you describe, it really looks like a display bug indeed, although I don't see why you would have it on smaller gridviews , a 10x20 table is no big deal, unless you're talking about very very limited client computer... – Laurent S. May 21 '13 at 16:51
  • @Bartdude Yes, thats what I feared. These computers are not actually that low spec and I have noticed it happen on my computer a few times but nowhere near as much. If that is the case there isnt really anything I can do unless double buffering? – manicmonkey21421 May 21 '13 at 16:55
  • are there line breaks in the data? Maybe the data is being displayed, it's just not visible due to clipping. – jugg1es May 21 '13 at 17:45
  • @jugg1es No, it was literally just random cells. I have managed to fix the issue (I believe) by using a custom datagridview with the double buffering property set to true. I will post it as the answer as soon as I know the issue has been fixed. Thanks for all your help! – manicmonkey21421 May 22 '13 at 07:47

1 Answers1

0

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.

manicmonkey21421
  • 113
  • 2
  • 12