问题
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.
回答1:
Check the formatting options, especially the Fill
-related properties. Adjusting columnwidths for all rows involves a lot of calculation.
回答2:
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
回答3:
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
回答4:
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
回答5:
for me changing RowHeadersWidthSizeMode
from:
DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
to:
DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
was extremely helpfull.
回答6:
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
来源:https://stackoverflow.com/questions/4660420/slow-to-set-datagridview-datasource-to-datatable-in-c-sharp