Friday, September 28, 2007

Use of BeginUpdate() and EndUpdate() when populating ListView/TreeView

Often a listview control in C# needs to be populated with large amount of data (bulky operations, for example, adding 1000 items). For every item added in the Listview, the control will redraw itself, thus greatly reduce the overall performance.

However, using the two methods BeginUpdate() and EndUpdate() while performing these bulk operations gives significant advantage. A call to BeginUpdate() can be made before adding/deleting/clearing items. This will stop any paint messages being sent or processed. Once the operation is done, the EndUpdate() can then be called.


I have logged the time taken of the above code (i.e. adding 1000 items in a ListView for each button click) with and without the BeginUpdate/EndUpdate method calls using a test application.


The use BeginUpdate() and EndUpdate populates the control with a constant duration and much better performance. Amazing, isn't it?

2 comments:

Kamlekar Venkateshwar said...

I am sorry I am new to c#. what do you mean by "redraw" here. Can you explain it please? Thanks in advance.

Ashvin Gunga said...

Kamlekar, here I mean it means "re-paint"...