|
1 | | -# How-to-perform-range-selection-with-shift-key-in-.NET-MAUI-DataGrid-SfDataGrid |
2 | | -This demo shows how to perform range selection with shift key in .NET MAUI DataGrid (SfDataGrid)? |
| 1 | +# How to perform range selection with shift key in .NET MAUI DataGrid SfDataGrid |
| 2 | +This article shows how to implement **Range Selection** with the **Shift key** in Syncfusion [.NET MAUI DataGrid](https://help.syncfusion.com/maui/datagrid/overview) (`SfDataGrid`). It demonstrates how a user can select rows continuously using the Shift key and the Arrow Up key. Similarly, deselection of rows continuously can also be done using the Shift key and the Arrow Down key. |
| 3 | + |
| 4 | +## Xaml |
| 5 | +``` |
| 6 | +<ContentPage.BindingContext> |
| 7 | + <local:OrderInfoRepository x:Name="viewModel" /> |
| 8 | +</ContentPage.BindingContext> |
| 9 | +
|
| 10 | +<ContentPage.Content> |
| 11 | + <syncfusion:SfDataGrid x:Name="dataGrid" |
| 12 | + SelectionMode="Multiple" |
| 13 | + ItemsSource="{Binding OrderInfoCollection}"> |
| 14 | + </syncfusion:SfDataGrid> |
| 15 | +</ContentPage.Content> |
| 16 | +``` |
| 17 | + |
| 18 | +## Xaml.cs |
| 19 | +``` |
| 20 | +public partial class MainPage : ContentPage |
| 21 | +{ |
| 22 | + readonly CustomRowSelectionController customRowSelectionController; |
| 23 | +
|
| 24 | + public MainPage() |
| 25 | + { |
| 26 | + InitializeComponent(); |
| 27 | + customRowSelectionController = new CustomRowSelectionController(dataGrid); |
| 28 | + dataGrid.SelectionController = customRowSelectionController; |
| 29 | +#if WINDOWS |
| 30 | + dataGrid.HandlerChanged += DataGrid_HandlerChanged; |
| 31 | +#endif |
| 32 | + } |
| 33 | +#if WINDOWS |
| 34 | + private void DataGrid_HandlerChanged(object? sender, EventArgs e) |
| 35 | + { |
| 36 | + if (dataGrid?.Handler?.PlatformView is FrameworkElement nativeElement) |
| 37 | + { |
| 38 | + nativeElement.KeyUp += DataGrid_KeyUpEvent; |
| 39 | + nativeElement.KeyDown += DataGrid_KeyDownEvent; |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | + private void DataGrid_KeyUpEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
| 44 | + { |
| 45 | + if (e.Key == Windows.System.VirtualKey.Shift) |
| 46 | + { |
| 47 | + customRowSelectionController._isShiftPressed = false; |
| 48 | + } |
| 49 | + } |
| 50 | +
|
| 51 | + private void DataGrid_KeyDownEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
| 52 | + { |
| 53 | + if (e.Key == Windows.System.VirtualKey.Shift) |
| 54 | + { |
| 55 | + customRowSelectionController._isShiftPressed = true; |
| 56 | + } |
| 57 | + } |
| 58 | +#endif |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## CustomRowSelectionController.cs |
| 63 | +``` |
| 64 | +public class CustomRowSelectionController : DataGridRowSelectionController |
| 65 | +{ |
| 66 | + private SfDataGrid grid; |
| 67 | + public bool _isShiftPressed { get; set; } |
| 68 | +
|
| 69 | + public CustomRowSelectionController(SfDataGrid dataGrid) : base(dataGrid) |
| 70 | + { |
| 71 | + this.grid = dataGrid; |
| 72 | + } |
| 73 | +
|
| 74 | + public override void HandlePointerOperation(RowColumnIndex rowColumnIndex) |
| 75 | + { |
| 76 | + if (_isShiftPressed) |
| 77 | + { |
| 78 | + if (grid.SelectedRows.Count > 0) |
| 79 | + { |
| 80 | + var selectedRow = grid.SelectedRows[0] as OrderInfo; |
| 81 | + var selectCollection = new ObservableCollection<object>(); |
| 82 | + var startIndex = (grid.ItemsSource as ObservableCollection<OrderInfo>)!.IndexOf(selectedRow); |
| 83 | + var endIndex = grid.ResolveToRecordIndex(rowColumnIndex.RowIndex); |
| 84 | + for (int i = startIndex; i <= endIndex; i++) |
| 85 | + { |
| 86 | + selectCollection.Add(grid.View!.Records[i].Data); |
| 87 | + } |
| 88 | + grid.SelectedRows = selectCollection; |
| 89 | + return; |
| 90 | + } |
| 91 | +
|
| 92 | + } |
| 93 | + base.HandlePointerOperation(rowColumnIndex); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### ScreenShot |
| 99 | + |
| 100 | +Here is the expected output when executing the sample: |
| 101 | + |
| 102 | +<img src="https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjQwOTc0Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.je6YEmZlMfStikxEWG73znnJzUvvE8_G1T-RQfMlTUg" width = 404 height = 396/> |
| 103 | + |
| 104 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-perform-range-selection-with-shift-key-in-.NET-MAUI-DataGrid-SfDataGrid) |
| 105 | + |
| 106 | + Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid). |
| 107 | + |
| 108 | +### Conclusion |
| 109 | +I hope you enjoyed learning about How to implement select all checkbox column in SfDataGrid. |
| 110 | + |
| 111 | +You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data. For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components. |
| 112 | + |
| 113 | +If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums),[Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you! |
0 commit comments