C# Assigning a value to DataRow[“haswhatnots”] = hasWhatnots is awful slow

落爺英雄遲暮 提交于 2019-12-11 09:56:35

问题


C# Assigning a value to DataRow["haswhatnots"] = hasWhatnots is awful slow. hasWhatnots is a boolean value.

I have profiled this line and with 560000 hits the execution time is 82 seconds. Of course the profiler has some effect on the performance, but still the performance of this is grazy slow!

Any hints on the issue. The DataRow is part of a DataTable which is bound to BindingSource that is bound to DataGridView.Datasource.


回答1:


(edit: only just saw that you are data-binding) The first thing to try is disabling data-binding; perhaps set the source to null and re-bind afterwards. BindingSource has SuspendBinding(), ResumeBinding() and ResetBindings() for this.


If the real problem is just lookup, you could take a snap of the DataColumn, and use:

// early code, once only...
DataColumn col = table.Columns["haswhatnots"];

// "real" code, perhaps in a loop
row[col] = hasWhatnots;

I seem to recall that this is the fastest route (the string overload locates the DataColumn from the list).

Alternatively - use a class model instead of DataTable ;-p




回答2:


You could try this

bindingSource1.RaiseListChangedEvents = false;

// stuff the grid

bindingSource1.RaiseListChangedEvents = true;

and see if it makes a difference.




回答3:


Very late, but still had the same issue

DataRow row

row.BeginEdit();
row["haswhatnots"] = hasWhatnots;
row.EndEdit();

There were extreme lags, occuring on (in my scale) large grids (60 cols, 10k+ rows), and this cutted the cpu time to less then one percent to what it was before.



来源:https://stackoverflow.com/questions/1729062/c-sharp-assigning-a-value-to-datarowhaswhatnots-haswhatnots-is-awful-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!