DataGridView 的图形故障
Graphical glitch of DataGridView
在我的客户报告我的软件出现图形故障后,我制作了一个示例项目,我设法轻松地重现了该问题。我在表单中创建了一个简单的停靠 DataGridView,并用这样的随机数据填充它:
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;
现在我启动我的程序,移动 window 以便它的一部分被我的任务栏覆盖并使用滚动条。突然所有数据都乱了:
更新:
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;
}
这背后的原因是什么?
因此,可以使用 .Invalidate()
方法作为解决方案。虽然这不是最好的方法。有关无效方法的更多信息,请参见此处:
https://msdn.microsoft.com/en-us/library/598t492a(v=vs.110).aspx
就我而言,问题在于
一些 DataGridView 单元格背景颜色设置为透明
g[x,y].Style.BackColor = Color.Transparent;
在我的客户报告我的软件出现图形故障后,我制作了一个示例项目,我设法轻松地重现了该问题。我在表单中创建了一个简单的停靠 DataGridView,并用这样的随机数据填充它:
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;
现在我启动我的程序,移动 window 以便它的一部分被我的任务栏覆盖并使用滚动条。突然所有数据都乱了:
更新:
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;
}
这背后的原因是什么?
因此,可以使用 .Invalidate()
方法作为解决方案。虽然这不是最好的方法。有关无效方法的更多信息,请参见此处:
https://msdn.microsoft.com/en-us/library/598t492a(v=vs.110).aspx
就我而言,问题在于 一些 DataGridView 单元格背景颜色设置为透明
g[x,y].Style.BackColor = Color.Transparent;