How to add selection to data rows of each group on expanding its CaptionSummaryRow?

SfDataGrid allows you to enable Grouping, by setting the AllowGrouping property at SfDataGrid or Column level. Each group contains its ownGroupCaptionRow and you can get the underlying records by expanding it.

The following screenshot displays two levels of grouped columns in SfDataGrid.

Figure 1 : Default structure of Grouping and its elements

By default, the selection is maintained in CaptionSummaryRow when the group is expanded or collapsed. You can customize and maintain that selection in its underlying records also, while expanding or collapsing the corresponding group.

You can achieve this byderiving new class from GridSelectionController class and by overriding its HandlePointerOperations virtual method in SfDataGrid.

Instantiate the instance of new class GridSelectionControllerExtthat is derived from theGridSelectionControllerto SfDataGrid’sSelectionControllerproperty asillustrated in the following code example.

C#

UsingHandlePointerOperations method, you can customize the selection when the pointer is released on CaptionSummaryRow as illustrated in the following code example.

C#

In the above code example, the selection is processed when the currently selected record type is a Group .You can get the currently selected record from the DisplayElements of TopLevelGroup by using its corresponding record index that is resolved from the current row index of the HandlePointerOperations virtual methods.

After getting the expanding group, you can process and give the selection to underlying records by using theCheckGroup() helper method that holds the corresponding group as argument. You can process the selection for both records and CaptionSummaryRow as illustrated in the following code example.

C#

In the above code example, check whether the next level of parent group contains records or sub groups. When the next level is records, the selection is addedby using SelectRows (intStartIndex, intEndIndex) method.Set thecurrent CaptionSummaryRow index as startRowIndex and the end of record collection as endRowIndex. The startRowIndex and the endRowIndex is resolved from record index of CaptionSummaryRow and records.

When the next level contains group ,the CheckGroup() method is called recursively until the records are found and the selection is processed by passing the parent group index as startRowIndex and end of sub groups as endRowIndex in SelectRows() method .

The following screenshot illustrates thecustomized selection of CaptionSummaryRow and Records while expanding group.

Figure 2 : Selection added to records of expanded group

Note: To maintain multiple row selection, set the SelectionModeas Multiple.

Refer to the following sample link for customize selection of data rows of each group while expanding,