C#中将DataGridView数据源设置为DataTable很慢

发布于 2024-10-12 02:34:07 字数 204 浏览 6 评论 0 原文

我有一个完全填充的 DataTable,我想将其设置为 DatagridView:

gdv.DataSource = dt;

但是,这非常慢。数据表的填充速度非常快,但仅上面这一行就需要很长时间。有什么方法可以加快速度或在另一个线程中执行它?

此后不再有任何交互。就是上面简单的一句话!

谢谢。

I have a DataTable, fully populated, which I want to set to a DatagridView:

gdv.DataSource = dt;

However, this is painfully slow. The filling of the DataTable is very quick, but just this one line above takes ages. Is there any way to speed this up or perform it in another thread?

There is no interaction after this point. Just the simple statement above!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤独患者 2024-10-19 02:34:07

检查格式选项,尤其是与 Fill 相关的属性。这些是 AutoSizeColumnMode 和各个列样式。

调整所有行的列宽涉及大量计算。

Check the formatting options, especially the Fill -related properties. Those are AutoSizeColumnMode and the individual column styles.

Adjusting columnwidths for all rows involves a lot of calculation.

束缚m 2024-10-19 02:34:07

这是一个修复。问题在于框架在新数据源中每行重新调整一次列大小(为什么?)。当然,它每次都需要循环所有行,从而导致 O(n^2) 操作。遗憾的是,您必须在设置数据源之前关闭自动调整大小,然后手动调用 AutoResizeColumns 方法。

grdChanges.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
grdChanges.DataSource = schemaChangesSpreadsheet.Changes
grdChanges.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCells)

事实证明,Microsoft 在一篇文章“扩展 Windows 窗体 DataGridView 控件的最佳实践”中告诉您这样做,如果您的 UI 设置具有相同的繁重计算问题,该文章可能会对您有所帮助。

http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

Here is a fix. The problem is that the framework re-resizes the columns once per row in the new datasource (why??). And of course it needs to loop over all rows each time, resulting in an O(n^2) operation. So sadly it looks like you must turn off autoresize before setting the datasource, then manually call the AutoResizeColumns method.

grdChanges.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
grdChanges.DataSource = schemaChangesSpreadsheet.Changes
grdChanges.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCells)

Turns out that Microsoft tells you to do this in an article "Best Practices for Scaling the Windows Forms DataGridView Control" which might help you if you have a different UI setting that has the same heavy computation issue.

http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

夏尔 2024-10-19 02:34:07

通过这段代码,我得到了很好的结果:

dtgvPlanificado.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dtgvPlanificado.ColumnHeadersVisible = false;
dtgvPlanificado.DataSource = DS.Tables("LV1");  
dtgvPlanificado.ColumnHeadersVisible = true;  
dtgvPlanificado.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

With this code I have good results:

dtgvPlanificado.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dtgvPlanificado.ColumnHeadersVisible = false;
dtgvPlanificado.DataSource = DS.Tables("LV1");  
dtgvPlanificado.ColumnHeadersVisible = true;  
dtgvPlanificado.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
携君以终年 2024-10-19 02:34:07

AutoSizeColumnsMode 是真正的瓶颈……11 秒变成了 15 毫秒。

这是我一直在寻找的:

<System.Runtime.CompilerServices.Extension()>
Public Sub BeginLoadData(dataGridView As DataGridView)
    dataGridView.Tag = dataGridView.AutoSizeColumnsMode
    dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
End Sub

<System.Runtime.CompilerServices.Extension()>
Public Sub EndLoadData(dataGridView As DataGridView)
    dataGridView.AutoSizeColumnsMode = CType(dataGridView.Tag, DataGridViewAutoSizeColumnsMode)
End Sub

AutoSizeColumnsMode is the real bottleneck ... and 11 seconds become 15 ms.

Here is what I was looking for:

<System.Runtime.CompilerServices.Extension()>
Public Sub BeginLoadData(dataGridView As DataGridView)
    dataGridView.Tag = dataGridView.AutoSizeColumnsMode
    dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
End Sub

<System.Runtime.CompilerServices.Extension()>
Public Sub EndLoadData(dataGridView As DataGridView)
    dataGridView.AutoSizeColumnsMode = CType(dataGridView.Tag, DataGridViewAutoSizeColumnsMode)
End Sub
等风来 2024-10-19 02:34:07

对我来说,将 RowHeadersWidthSizeMode 从: 更改

DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;

为:

DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;

非常有帮助。

for me changing RowHeadersWidthSizeMode from:

DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;

to:

DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;

was extremely helpfull.

你的笑 2024-10-19 02:34:07

不要忘记自动调整行大小也可以发挥作用。这对我来说效果很好。将数据绑定时间从约 1 秒缩短到 0.1 秒。我希望一切都那么容易获得 10 倍的速度!

dgvProperties.DataSource = Nothing
dgvProperties.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
dgvProperties.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
dgvProperties.DataSource = datatable
dgvProperties.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dgvProperties.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

Don't forget that the autosize rows can also play a role. This works well for me. Took the databinding from ~1 sec to 0.1 sec. I wish everything was that easy to get a 10x speed up!

dgvProperties.DataSource = Nothing
dgvProperties.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
dgvProperties.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
dgvProperties.DataSource = datatable
dgvProperties.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dgvProperties.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文