如何优化的DataGridView的性能性能、DataGridView

2023-09-04 23:01:56 作者:尘中客忘归家

我有我的Windows应用程序的形式DataGridView控件。此的DataGridView填充一个纯文本文件(由用户在运行时指定)的基础上。因此列和行的数量是动态计算的。现在的每一件事工作正常,预期唯一的问题是,我的DataGridView花了很多时间来加载数据,有没有什么办法来优化DataGridView中的表现?

提示:正常情况下,datagridview的1024,则为列和Almos酒店100行

以下是code来填充我的DataGridView

  dataGridView1.ColumnCount = nColumnCount;

的for(int i = 0; I< CurrPageLines.Length;我++)
{
    字符串sCurrLinecontents = CurrPageLines [I]
    INT N = dataGridView1.Rows.Add();
    对于(INT J = 0; J< /*nColumnCount*/sCurrLinecontents.Length; J ++)
    {
        dataGridView1.Rows [N] .Cells [J]。价值= sCurrLinecontents [J]。
    }
}
 

解决方案

有上的如何使用的datagridview 的虚拟模式。它甚至还带有一个很好的演练例子。虽然它似乎是更有针对性的对大量的行,而不是大量列的可能仍然是有用的。

更新:如果你是刚刚经历了加载数据时的延迟,你也许可以通过创建一个数据表或的BindingList 从文本文件,然后将绑定到视图。

I have a DataGridView control on my windows application form. This DataGridView is populated on the basis of a plain text file (specified by user at run time). Hence the number of columns and rows are calculated dynamically. Now the every thing works fine as expected the only issue is that my DataGridView took a lot of time to load data, is there any way to optimize the performance of DataGridView?

c 关于datagridview属性设置

Hint: Normally the datagridview contians 1024 columns and almos 100 rows.

Following is the code for populating my DataGridView

dataGridView1.ColumnCount = nColumnCount;

for (int i = 0; i < CurrPageLines.Length; i++)
{
    string sCurrLinecontents = CurrPageLines[i];
    int n = dataGridView1.Rows.Add();
    for (int j = 0; j < /*nColumnCount*/sCurrLinecontents.Length; j++)
    {
        dataGridView1.Rows[n].Cells[j].Value = sCurrLinecontents[j];
    }
}

解决方案

There is an article on MSDN on how use the virtual mode of the datagridview. It even comes with a nice walkthrough example. Although it seems to be more targeted towards lots of rows instead of lots of columns it might still be useful.

Update: If you are just experiencing delays when loading data you might be able to improve things by creating a DataTable or a BindingList from your textfile and then bind that to the view.