Batch Modifications
- 2 minutes to read
The XtraEditors library supports batch modifications. You can perform a sequence of modifications affecting the editor’s appearance, and only update an editor, giving you a significant performance increase. This topic explains the batch modifications concept and provides examples demonstrating the technique.
The Batch Modifications Concept
Normally, each operation affecting the editor’s appearance forces the editor to recalculate its view information and repaint itself. As stated above, you may need to avoid this when performing several changes. To do this, the code performing the changes must be enclosed within the RepositoryItem.BeginUpdate/RepositoryItem.EndUpdate or RepositoryItem.BeginUpdate/RepositoryItem.CancelUpdate method calls. The RepositoryItem.BeginUpdate method increments an internal counter, initially zero) by one. The RepositoryItem.EndUpdate and RepositoryItem.CancelUpdate methods decrement this counter. The editor’s view information recalculation and repainting is only performed when the counter value is zero.So, the code enclosed into the method pairs above does not force editor updates after each operation.
Since the RepositoryItem.BeginUpdate, RepositoryItem.EndUpdate and RepositoryItem.CancelUpdate methods use a counter, nesting the calls to these methods is allowed. However, there must be the same number of RepositoryItem.BeginUpdate and RepositoryItem.EndUpdate/RepositoryItem.CancelUpdate method calls. If the number of method calls that end batch modification is less than RepositoryItem.BeginUpdate method calls, the editor will not update itself further.
It is a common technique to use the try...finally
block to ensure that a RepositoryItem.EndUpdate/RepositoryItem.CancelUpdate method call is actually performed to avoid locking the editor .
Comparing the EndUpdate and CancelUpdate Methods
If the RepositoryItem.EndUpdate method is called to unlock an editor, the editor is updated immediately. Its view information is recalculated and it is repainted to reflect the changes made. On the other hand, the RepositoryItem.CancelUpdate method does not force an immediate update. Use this method only if you are sure that the editor’s appearance remains the same after all the changes have been applied. This enables you to avoid an unnecessary update.
The sample code below shows an example of using the RepositoryItem.BeginUpdate and RepositoryItem.EndUpdate methods. These methods prevent a ButtonEdit from being updated while changing the position of its buttons.
buttonEdit1.Properties.BeginUpdate();
try {
for(int i = 0; i < buttonEdit1.Properties.Buttons.Count; i++)
buttonEdit1.Properties.Buttons[i].IsLeft = true;
}
finally {
buttonEdit1.Properties.EndUpdate();
}
The following image shows the editor’s appearance before and after the sample code’s execution.