Skip to main content
A newer version of this page is available. .

Batch Modifications

  • 4 minutes to read

Batch modifications are designed to speed up a grid control’s performance by eliminating superfluous visual and data updates. The main objective is to update the grid control only once - after all the necessary changes have been made.

Preventing Excessive Visual Updates

Every time you change a property or call a method that affects a grid’s visual appearance, it is updated to reflect its new state. In some cases, changing a property only affects the corresponding display element, not the entire grid control, and so only this element is repainted. In most cases however, modifying a property or calling a method affects an entire grid control, and therefore the grid is updated. To update a grid, the VGridControlBase.LayoutChanged method is called.

If you perform a sequence of modifications that cause grid updates, you may notice that performance suffers, especially when these operations are time-consuming. For instance, deleting multiple records may take a while, because the grid is updated after every deletion. However, you can speed up this operation by preventing unnecessary updates between individual operations. The grid control provides several methods to support these batch modifications.

Before applying a series of changes to a grid, you can call the VGridControlBase.BeginUpdate method. This locks the grid and prevents subsequent visual updates. After you’ve performed all the necessary operations on a grid, call the VGridControlBase.EndUpdate or VGridControlBase.CancelUpdate method. The EndUpdate method immediately updates the grid to reflect all recent changes, and re-enables it for future updates. The CancelUpdate method re-enables the grid control for future updates, but it does not cause an immediate visual update.

The BeginUpdate and EndUpdate/CancelUpdate methods use an internal counter to implement the appropriate functionality. The counter has an initial value of 0. Each call to the BeginUpdate method increments this counter by one. Each call to the EndUpdate/CancelUpdate method decrements this counter by one and if its new value is zero, the grid is updated. Note that each call to BeginUpdate must be paired with a call to EndUpdate/CancelUpdate. If you call BeginUpdate, but forget to call EndUpdate/CancelUpdate afterwards or EndUpdate/CancelUpdate isn’t called because an exception occurred, the grid will no longer be refreshed. To ensure that EndUpdate/CancelUpdate is always called even when an exception occurs, use the try…finally statement.

Example

The sample code below shows an example of using the batch modifications technique. The SetRowsHeight method can be used to prevent the vertical grid from being updated while changing the height of its rows.

using DevExpress.XtraVerticalGrid.Rows;
// ...
private void SetRowsHeight(int height){
    BaseRow row;
    vGridControl1.BeginUpdate();
    try{
        row = vGridControl1.GetFirst();
        while (row != null){
            row.Height = height;
            row = vGridControl1.GetNext(row);
        }
    }
    finally{
        vGridControl1.EndUpdate();
    }
}

Preventing Excessive Internal Data Updates

The BeginUpdate and EndUpdate methods prevent only visual updates. When you change a property or call a method within a BeginUpdate and EndUpdate pair, the corresponding actions take effect immediately. However, the results are displayed only after EndUpdate has been called.

Some operations you may perform force the grid control to reload data. Each of these operations causes an internal data update, and when such operations are performed in a sequence, multiple data updates occur. Excessive data updates cannot be avoided by using the BeginUpdate and EndUpdate methods. Instead, the VGridControlBase.BeginDataUpdate and VGridControlBase.EndDataUpdate methods must be used. These methods improve a control’s performance when a sequence of any of the following operations is performed:

  • adding, deleting, or modifying records at the data source level.

If the code that performs a sequence of any of these operations is wrapped with the BeginDataUpdate and EndDataUpdate methods, the grid will perform only a single data update reflecting all changes made after the EndDataUpdate method is called.

Use the BeginDataUpdate and EndDataUpdate methods in a similar way to the BeginUpdate and EndUpdate methods. Each call to the BeginDataUpdate method must correspond to a call to the EndDataUpdate method. To ensure that this happens (even if an exception occurs), use the try…finally block.

The BeginDataUpdate and EndDataUpdate methods call the BeginUpdate and EndUpdate methods, respectively. So, you don’t need to call these methods to prevent superfluous visual updates when using the BeginDataUpdate and EndDataUpdate methods.