Articles in this section
Category / Section

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

11 mins read
WPF DataGrid (SfDataGrid) allows you to enable Grouping, by setting the DataGrid.AllowGrouping property or GridColumn.AllowGrouping. Each group contains its own GroupCaptionRow and you can get the underlying records by expanding it.

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



 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 by deriving new class from GridSelectionController class and by overriding its HandlePointerOperations virtual method in DataGrid. Instantiate the instance of new class GridSelectionControllerExt that is derived from the GridSelectionController to DataGrid’s SelectionController property as illustrated in the following code example.
public MainWindow()
{
   InitializeComponent();
   //Customized SelectionController is assigned to SelectionController of SfDataGrid
   this.sfdatagrid.SelectionController = new GridSelectionControllerExt(sfdatagrid);
}

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

public class GridSelectionControllerExt : GridSelectionController
{
     public GridSelectionControllerExt(SfDataGrid dataGrid)
     : base(dataGrid)
     {
     }
     public override void HandlePointerOperations(GridPointerEventArgs args, Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex rowColumnIndex)
     {
         base.HandlePointerOperations(args, rowColumnIndex);
         //Check whether the operation is pointer released or not
         if (args.Operation == PointerOperation.Released)
         {
             if (this.DataGrid.View.TopLevelGroup != null)
             {
                 //Get the group from the DisplayElements by resolving  record index of corresponding   row index
                 var group = this.DataGrid.View.TopLevelGroup.DisplayElements[this.DataGrid.ResolveToRecordIndex(ro wColumnIndex.RowIndex)];
                 //Check whether the group is null or not and process selection, when the selected record  type is Group
                 if (group != null && group is Group)
                 {
                     var gr = group as Group;
                     //Process selection, by checking group and its record
                     CheckGroup(gr);
                 }
             }
         }
     }
}
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 the CheckGroup 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.

private void CheckGroup(Group group)
{
    //Check whether the group is null or not
    if (group != null)
    {
        //Check whether the Group is null, that means the next level contains records and it’s in expanded state
        if (group.Groups == null && group.IsExpanded)
        {
            //Get the corresponding start index of record by getting it from DisplayElements .
            var startindex = this.DataGrid.View.TopLevelGroup.DisplayElements.IndexOf(group as Group);
            //Resolve the RowIndex of record
            var startRowIndex = this.DataGrid.ResolveToRowIndex(startindex);
            //Initialize end index of record
            var endIndex = 0;
            //Process the selection to the total number of records in each group by getting its total record data
            foreach (var rec in group.Records)
            {
                //Get the corresponding end index of record by getting it from DisplayElements .
                endIndex = this.DataGrid.View.TopLevelGroup.DisplayElements.IndexOf(rec);
            }
            //Resolve the row index of end index
            var endRowIndex = this.DataGrid.ResolveToRowIndex(endIndex);
            //Select the rows from corresponding start and end row index
            this.DataGrid.SelectionController.SelectRows(startRowIndex, endRowIndex);
        }
        //Check whether the group is expanded
        else if (group.IsExpanded)
        {
            //if more than one group is achieved ,you need to check those levels and get the records
            foreach (var gr in group.Groups)
            {
                //Called recursively , to traverse it inner level of group.
                CheckGroup(gr);
                //Maintain the selection in Caption Summary Row while the group is in expanded state.
                //Get the corresponding start index of caption summary row by getting it from DisplayElements .
                var startindex = this.DataGrid.View.TopLevelGroup.DisplayElements.IndexOf(group as Group);
                //Resolve the RowIndex of caption summary row
                var startRowIndex = this.DataGrid.ResolveToRowIndex(startindex);
                //Get the corresponding end index of caption summary row by getting it from DisplayElements .
                var endindex = this.DataGrid.View.TopLevelGroup.DisplayElements.IndexOf(gr as Group);
                //Resolve the RowIndex of caption summary row
                var endRowIndex = this.DataGrid.ResolveToRowIndex(endindex);
                //Select the rows from corresponding start and end row index
                this.DataGrid.SelectionController.SelectRows(startRowIndex, endRowIndex);
            }
        }
    }
}
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 added by using SelectRows method. Set the current 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 subgroups as endRowIndex in SelectRows method.


The following screenshot illustrates the customized 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 SelectionMode as Multiple.

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

Conclusion
I hope you enjoyed learning about how to add selection to data rows of each group on expanding its CaptionSummaryRow in DataGrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied