C# Assigning a value to DataRow[“haswhatnots”] = h

2019-08-05 19:09发布

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.

3条回答
Luminary・发光体
2楼-- · 2019-08-05 19:50

You could try this

bindingSource1.RaiseListChangedEvents = false;

// stuff the grid

bindingSource1.RaiseListChangedEvents = true;

and see if it makes a difference.

查看更多
虎瘦雄心在
3楼-- · 2019-08-05 20:06

(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

查看更多
Juvenile、少年°
4楼-- · 2019-08-05 20:06

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.

查看更多
登录 后发表回答