Optimizing Sorting Update Performance in XamDataGrid

Kiril Matev / Tuesday, April 10, 2012

The XamDataGrid , the grid control which comes as a part of the NetAdvantage for WPF Line of Business product, has been in use for a few years in a variety of scenarios – from customizability and rich templating, to high data density and high-performance. Recently, we have made an important addition to its API, which lets developers handle sorting in update scenarios without sacrificing any aspect of performance (detailed performance optimization guidance is available here). This blog post describes this new capability and illustrates its implementation in a sample project. It also demonstrates its importance by comparing the number of calls to the repositioning logic made with the old and new approaches to handling record repositioning.

Please see the screenshot below and note the 7 times fewer invocations of repositioning logic using the new approach. The reason for this is that record repositioning is only done in response to an update on the sorted field, rather than every time there's an update on any of the fields of the record. This is why the performance improvement you will see will depend on the number of updated fields - the more fields you have updating, the bigger the performance improvement of using the new approach.

Handling record updates (old approach)

By default, the XamDataGrid doesn’t reposition a record in a sorted or grouped grid once its sorted/grouped property value changes. In this way, the XamDataGrid lets the developer have full control over the timing of the repositioning operation and doesn’t sacrifice any performance by automatic repositioning in case it’s not needed by the scenario. This applies when sorting and grouping is activated from the XamDataGrid itself, rather than bound to a data source which sorts the values intrinsically such as a ListCollectionView which has sort-descriptors set. In the latter case, the ListCollectionView handles the sorting and the XamDataGrid repositions the records automatically.

In case you’re not using a ListCollectionView, but the XamDataGrid sorting instead, you’ll have to reposition records manually when there’s an update in response to the user changing the value of the sorted column, or an update coming in from the data source. This requires the developer to handle the InitializeRecord routed event or its plain CLR event counterpart - InitializeRecordDirect where the RefreshSortPosition method is called on the record. Until now, this was typically performed using the code below:

private void xamDataGrid1_InitializeRecordDirect(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)
{      
     //we have to call the RefreshSortPosition method, or else the records do not get their group placement updated after a data update      
if (e.ReInitialize)     
{         
//logic to be performed before repositioning         
e.Record.RefreshSortPosition();
//logic to be performed before repositioning           
     }
}

Handling record updates (new approach)

The InitializeRecord event is called every time there is a change in any of the record’s fields. This means that if we have a record containing 10 fields, all of which are updated by the user or by a background process, the record repositioning logic is invoked regardless of whether the column the user has sorted on was updated. This is a rather wasteful approach, because assuming we have sorted by one of these fields, the repositioning logic is called 10 times, instead of just once in response to the sorted column being updated. This is why in the currently available service release version of the 11.2 release (build 2125), we have added the SortValueChanged property to the InitializeRecordEventArgs which lets the developer know whether the record is initialized because sorted column value has changed, allowing them to call the repositioning logic only when required. This new property appears in all subsequent versions, including the forthcoming 12.1 release. This changes the code above into the following:

private void xamDataGrid1_InitializeRecordDirect(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)
{
     //we have to call the RefreshSortPosition method, or else the records do not get their group placement updated after a data update
     if (e.SortValueChanged)
     {
         //logic to be performed before repositioning
         e.Record.RefreshSortPosition();
//logic to be performed before repositioning
     }
}

Summary

In this blogpost, I described the new API introduced in the 11.2 service release of the NetAdvantage for WPF Line of Business product, which lets you dramatically cut down the number of calls to repositioning logic in the XamDataGrid in response to data updates. This approach will let you achieve a finer level of control over when record positioning logic is invoked, and ultimately improve performance in these scenarios.