Search code examples
c#winformsdatagridviewvisual-glitch

Graphical glitch of DataGridView


I have made a sample project after my customers reported a graphical glitch in my software and I managed to reproduce the problem with ease. I create a simple docked DataGridView in a form and I fill it with random data like this:

var ds = new DataSet();
var table = ds.Tables.Add();
Enumerable.Range(0, 100).ForEach(i => table.Columns.Add(i.ToString()));

Enumerable.Range(0, 100).ForEach(i =>
{
    var row = table.NewRow();
    Enumerable.Range(0, 100).ForEach(j => row[j.ToString()] = Guid.NewGuid().ToString());
    table.Rows.Add(row);
});

dataGridView1.DataSource = table;

Now I launch my program, move the window so that a part of it is covered by my taskbar and use a scrollbar. Suddenly all the data is messed up:

UPDATE:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(986, 758);
        this.dataGridView1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(986, 758);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
}

enter image description here

What is the reason behind this?


Solution

  • So as a solution the .Invalidate() method can be used. Although it is not the best way. More about invalidate method can be found here: https://msdn.microsoft.com/en-us/library/598t492a(v=vs.110).aspx