DataGridView的加载速度很慢。如何优化DataGridView中添加行的? [英] The DataGridView is loading very slowly. How to optimise adding of rows in DataGridView?
问题描述
DataGridView的加载速度很慢。如何优化呢?
在DataGridView有4-5千行。
我要动态地生成一个DataGridView的一些参数。(从数据库中的数据,号列)
The datagridview has 4-5 thousand rows.
I have to generate a datagridview dynamically on a few parameters.(Data from database, No. of columns)
我要垂直从表(ID,姓名,联系方式)的数据库中生成像这样的DataGridView ..
I have to generate the datagridview like this vertically from the table (id,name,contact) in database..
列1
ID
名称
与
id
name
contact
现在可以有任意数量旁边的列1更多的空列。
Now there can be any number of more empty columns beside column1.
目前我下面这个方法。
Currently I am following this approach.
- 首先将所有的空列。
- 然后添加三行在每个循环迭代一行对每个(ID,姓名,联系方式)。
-
我从数据库中获取数据,并把它当作一个
名单,LT;字符串[]>
到GenerateRows
的功能。
private void GenerateColumns(int colLen)
{
dataGridViewGenerate.Rows.Clear();
dataGridViewGenerate.Columns.Clear();
DataGridViewColumn col0 = new DataGridViewTextBoxColumn();
col0.HeaderText = "Employee No. & Name";
dataGridViewGenerate.Columns.Add(col0);
for (int i = 0; i < colLen; i++)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn
{
HeaderText =
(_sTime.AddDays(i)).Day.ToString(CultureInfo.InvariantCulture) + " " +
(_sTime.AddDays(i)).ToString("ddd")
};
dataGridViewGenerate.Columns.Add(col);
}
private void GenerateRows(List<string[]> empList)
{
int len = empList.Count;
for (int a = 0; a < len; a++)
{
string[] arr = empList[a];
//row 1
var row1 = new DataGridViewRow();
row1.Cells.Add(new DataGridViewTextBoxCell
{
Value = arr[0]
});
dataGridViewGenerate.Rows.Add(row1);
//row 2
var row2 = new DataGridViewRow();
row2.Cells.Add(new DataGridViewTextBoxCell
{
Value = arr[1]
});
dataGridViewGenerate.Rows.Add(row2);
//row3
var row3 = new DataGridViewRow();
row3.Cells.Add(new DataGridViewTextBoxCell
{
Value = arr[2]
});
dataGridViewGenerate.Rows.Add(row3);
}
}
我想使SQL中的过程,它会创建一个表,并用数据填充它。然后,只需将数据源分配到DataGridView。的
推荐答案
是不是一个对的DataGridView
添加行之一。我刚刚创建了一个数据表
从我的数据,并赋予它作为一个数据源
到的DataGridView
。
What I did is instead of adding rows one by one to the DataGridView
. I just created a DataTable
from my data and assigned it as a DataSource
to the DataGridView
.
加载的DataGridView
的性能显著改善。
这篇关于DataGridView的加载速度很慢。如何优化DataGridView中添加行的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!