尝试禁用网格的所有事件处理程序,然后查看网格的执行情况。如果性能良好,请启用一些,直到性能受到影响。如果即使网格没有事件处理程序并且它仍然执行缓慢,罪魁祸首可能是史蒂夫在他的回答中提到的自动调整大小。
应用程序性能在其他机器上是否会受到影响?是否与需要重新安装视频驱动程序有关?
编辑:我刚刚做了一个测试应用程序并看到了你的问题,但是当我进行双缓冲时它就消失了?
你是如何做到双缓冲的?
看这个答案:如何在窗体上双缓冲 .NET 控件? https://stackoverflow.com/questions/76993/how-to-double-buffer-net-controls-on-a-form
我的完整代码,我做了一个DataSet
有 20 列称为DataSet1
,然后我制作了一个简单的 Windows 窗体DataGridView
:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// comment out the line below for the application to lag
SetDoubleBuffered(dataGridView1);
for (int i = 0; i < 10000; i++)
{
dataSet1.DataTable1.AddDataTable1Row(GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString(),
GetRandomString());
}
}
public static void SetDoubleBuffered(System.Windows.Forms.Control c)
{
//Taxes: Remote Desktop Connection and painting
//http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
return;
System.Reflection.PropertyInfo aProp =
typeof(System.Windows.Forms.Control).GetProperty(
"DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
aProp.SetValue(c, true, null);
}
private Random rand = new Random();
private string validChars = "0123456789abcdefghijklmnopqurstuvwyz";
private string GetRandomString()
{
StringBuilder builder = new StringBuilder();
char[] c = new char[rand.Next(15,20)];
for (int i = 0; i < c.Length; i++)
{
c[i] = validChars[rand.Next(0, validChars.Length - 1)];
}
return new string(c);
}
}
}
使用超过 100,000 条记录进行测试,每条记录有 20 列,长度从 15 到 20 不等