How to avoid selection while grouping and ungrouping in WPF DataGrid?
In WPF DataGrid, the selection is maintained based on SfDataGrid.SelectedItems when grouping and ungrouping. If SelectedItems is zero, then the SfDataGrid processes selection behavior as follows:
- While grouping, the first caption summary row is selected and also if the Groups are collapsed, then the first caption summary row is selected by clearing SelectedItems.
- While ungrouping, first record row is selected. Note:
This is applicable only when SfDataGrid.SelectionUnit is Row.
If there are no SelectedItems, you can avoid the above default selection behavior by overriding the GridSelectionController, when SfDataGrid.SelectionUnit is Row. If SelectionUnit is Cell or Any, then you can override GridCellSelectionController, that handles all the selection operations in SfDataGrid.
public class GridSelectionControllerExt : GridSelectionController { public GridSelectionControllerExt(SfDataGrid sfgrid) : base(sfgrid) { } }
You can set the customized GridSelectionController, GridSelectionControllerExt as per the above code snippet, to SfDataGrid.SelectionController property, as shown in the following code snippet.
this.sfgrid.SelectionController = new GridSelectionControllerExt(sfgrid);
GridSelectionController class has various virtual methods that are called for different actions. To handle the selection while grouping or ungrouping, you have to override ProcessOnGroupChanged method in your SelectionController, as shown in the following code snippet.
C#
protected override void ProcessOnGroupChanged(NotifyCollectionChangedEventArgs args) { this.SuspendUpdates(); var removedItems = new List<object>(); //Removes the items which are not in View this.RefreshSelectedItems(ref removedItems); // Resets the selected indexes based on SelectedItems this.RefreshSelectedRows(); // Update the current RowIndex this.UpdateCurrentRowIndex(); this.ResumeUpdates(); }
In the above code, the ProcessOnGroupChanged method is called while grouping or ungrouping a column in SfDataGrid. RefreshSelectedItems method removes the items that are not displayed in the View. Because, while grouping, if the groups are not expanded, the SelectedItems will be cleared and the first caption summary is selected by default. The UpdateCurrentRowIndex method updates CurrentCell based on CurrentItem.
Screenshot:
Figure 1: While grouping, the first caption summary row is selected
Figure 2: Avoid selection while grouping and ungrouping in SfDataGrid
Sample Links
Conclusion
I hope you enjoyed learning about how to avoid selection while grouping and ungrouping in WPF Datagrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation 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!