Using Custom Grouping in the Silverlight XamGrid

Kiril Matev / Wednesday, June 16, 2010

You’ve talked to your users about showing those inventory levels records in the NetAdvantage for Silverlight XamGrid. They’ve mentioned  they would like to see inventory stock levels categorized in groups corresponding to how soon they have to be restocked. Your users would like certain threshold values to be used on the number of items in stock for separating the inventory items into the different groups. However, standard grouping is applied using an equals operator, rather than membership in an interval. This blog post offers a solution for this scenario – it explains how to implement custom grouping logic that would provide your users with an easy-to-use inventory replenishment screen.

Setting up grouping in the XamGrid

It’s rather straightforward to setup the XamGrid to use grouping. Generally, we can use one of three approaches:

  • Use the GroupBySettings object to setup grouping for the entire XamGrid or on individual ColumnLayout objects
  • Add the column to the GroupByColumns collection
  • Set an individual column’s IsGroupBy property

The approach used in this sample is to set the IsGroupBy property of the UnitsInStock column to true.

Implementing custom grouping logic

We can implement the grouping logic by setting the GroupByComparer property of the column we’d like to group by using own comparer class - InventoryLevelsGroupByComparer. This class implements our custom logic in the GetHashCode method, which is set to return identical codes for values falling within the intervals specified by the threshold values in the StockLevels enumeration (which represent the threshold values our users provided us with). You can find the implementation of the grouping logic in the InventoryLevelsGroupByComparer.cs file.

Setting custom group headers

Having implemented custom grouping logic, we should set the group header values to reflect the meaning of the different groups. We can do that by changing the default group item template, and provide our own GroupByItemTemplate. We setup the group item template to use the InventoryLevelsGroupItemHeaderConverter to convert the boundary column value into a group header text. The group header text converter logic uses the same interval bounds as in the converter we used for determining the group a record will be a member of. You can find the logic of the group header text converter in the InventoryLevelsGroupItemHeaderConverter.cs file.

In order to make this sample a bit more complete, there’s a button in each row, which the user can use to restock the item by placing an order with the supplier. Clicking this button currently increases the value of the units in stock for the current record, and forces the grid to re-evaluate which group the record is a member of.

Summary

This blogpost demonstrated how you can enrich the data you have in your data sources by adding context to it to reflect the user’s work process and priorities. In this example, we used custom grouping in the XamGrid to place records into groups representing value ranges, which the user examines and acts upon as part of their duties. This should help you easily add custom grouping to enhance the data you’re presenting, make users more productive, and increase the value of your application.

If you have any questions, do not hesitate to email me at kmatev@infragistics.com