简体   繁体   English

在 C# 中将 DataGridView DataSource 设置为 DataTable 很慢

[英]Slow to set DataGridView DataSource to DataTable in C#

I have a DataTable, fully populated, which I want to set to a DatagridView:我有一个完全填充的 DataTable,我想将其设置为 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. DataTable 的填充非常快,但只是上面的这一行需要很长时间。 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.谢谢。

Check the formatting options, especially the Fill -related properties.检查格式选项,尤其是与Fill相关的属性。 Those are AutoSizeColumnMode and the individual column styles.这些是 AutoSizeColumnMode 和单独的列样式。

Adjusting columnwidths for all rows involves a lot of calculation.调整所有行的列宽涉及大量计算。

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.当然,它每次都需要遍历所有行,从而导致 O(n^2) 操作。 So sadly it looks like you must turn off autoresize before setting the datasource, then manually call the AutoResizeColumns method.很遗憾,您必须在设置数据源之前关闭自动调整大小,然后手动调用 AutoResizeColumns 方法。

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.事实证明,微软在一篇文章“缩放 Windows 窗体 DataGridView 控件的最佳实践”中告诉你这样做,如果你有一个不同的 UI 设置,它可能会帮助你,如果你有同样的繁重计算问题。

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

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;

AutoSizeColumnsMode is the real bottleneck ... and 11 seconds become 15 ms. AutoSizeColumnsMode 是真正的瓶颈……11 秒变成了 15 毫秒。

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

for me changing RowHeadersWidthSizeMode from:对我来说,将RowHeadersWidthSizeMode更改为:

DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;

to:到:

DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;

was extremely helpfull.非常有帮助。

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.将数据绑定从 ~1 秒缩短到 0.1 秒。 I wish everything was that easy to get a 10x speed up!我希望一切都那么容易获得 10 倍的加速!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM