gpt4 book ai didi

c# - 如何优化DataGridView的性能

转载 作者:行者123 更新时间:2023-12-04 02:21:51 24 4
gpt4 key购买 nike

我的 Windows 应用程序窗体上有一个 DataGridView 控件。此 DataGridView 是基于纯文本文件(由用户在运行时指定)填充的。因此,列数和行数是动态计算的。现在一切都按预期正常工作,唯一的问题是我的 DataGridView 加载数据花费了很多时间,有什么方法可以优化 DataGridView 的性能吗?

提示:通常 datagridview 包含 1024 列和大约 100 行。

以下是填充我的 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];
}
}

最佳答案

MSDN 上有一篇关于 how use the virtual mode of the datagridview 的文章.它甚至带有 a nice walkthrough example .尽管它似乎更针对大量行而不是大量列,但它可能仍然有用。

更新:如果您只是在加载数据时遇到延迟,您可以通过创建 DataTableBindingList 来改进您的文本文件,然后将其绑定(bind)到 View 。

关于c# - 如何优化DataGridView的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5659204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com