Skip to content

Commit 29ebe34

Browse files
Merge pull request #1 from SyncfusionExamples/MAUI-Datagrid-Selection
MAUI - 959526 - Added KB sample for range selection using shift key
2 parents c8c88dd + 5e6fcbf commit 29ebe34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+9336
-2
lines changed

README.md

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,113 @@
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!
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35806.99 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfDatagridSelection", "SfDatagridSelection\SfDatagridSelection.csproj", "{EFCE8E56-B139-4744-B03F-62059AE3A3FE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {45C581CD-38F1-4C2D-9A6E-E714D9D71D53}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDatagridSelection"
5+
x:Class="SfDatagridSelection.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace SfDatagridSelection
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
}
9+
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new Window(new AppShell());
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="SfDatagridSelection.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:SfDatagridSelection"
7+
Shell.FlyoutBehavior="Flyout"
8+
Title="SfDatagridSelection">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SfDatagridSelection
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Syncfusion.Maui.Data;
2+
using Syncfusion.Maui.DataGrid;
3+
using Syncfusion.Maui.DataGrid.Helper;
4+
using Syncfusion.Maui.GridCommon.ScrollAxis;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Collections.ObjectModel;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace SfDataGridSelection
13+
{
14+
15+
public class CustomRowSelectionController : DataGridRowSelectionController
16+
{
17+
private SfDataGrid grid;
18+
public bool _isShiftPressed { get; set; }
19+
20+
public CustomRowSelectionController(SfDataGrid dataGrid) : base(dataGrid)
21+
{
22+
this.grid = dataGrid;
23+
}
24+
25+
public override void HandlePointerOperation(RowColumnIndex rowColumnIndex)
26+
{
27+
if (_isShiftPressed)
28+
{
29+
if (grid.SelectedRows.Count > 0)
30+
{
31+
var selectedRow = grid.SelectedRows[0] as OrderInfo;
32+
var selectCollection = new ObservableCollection<object>();
33+
var startIndex = (grid.ItemsSource as ObservableCollection<OrderInfo>)!.IndexOf(selectedRow);
34+
var endIndex = grid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
35+
for (int i = startIndex; i <= endIndex; i++)
36+
{
37+
selectCollection.Add(grid.View!.Records[i].Data);
38+
}
39+
grid.SelectedRows = selectCollection;
40+
return;
41+
}
42+
43+
}
44+
base.HandlePointerOperation(rowColumnIndex);
45+
}
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="SfDatagridSelection.MainPage"
5+
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
6+
xmlns:local="clr-namespace:SfDataGridSelection">
7+
8+
<ContentPage.BindingContext>
9+
<local:OrderInfoRepository x:Name="viewModel" />
10+
</ContentPage.BindingContext>
11+
12+
<ContentPage.Content>
13+
<syncfusion:SfDataGrid x:Name="dataGrid"
14+
SelectionMode="Multiple"
15+
ItemsSource="{Binding OrderInfoCollection}">
16+
</syncfusion:SfDataGrid>
17+
</ContentPage.Content>
18+
19+
</ContentPage>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#if WINDOWS
2+
using Microsoft.UI.Xaml;
3+
#endif
4+
using SfDataGridSelection;
5+
6+
namespace SfDatagridSelection
7+
{
8+
public partial class MainPage : ContentPage
9+
{
10+
readonly CustomRowSelectionController customRowSelectionController;
11+
12+
public MainPage()
13+
{
14+
InitializeComponent();
15+
customRowSelectionController = new CustomRowSelectionController(dataGrid);
16+
dataGrid.SelectionController = customRowSelectionController;
17+
#if WINDOWS
18+
dataGrid.HandlerChanged += DataGrid_HandlerChanged;
19+
#endif
20+
}
21+
#if WINDOWS
22+
private void DataGrid_HandlerChanged(object? sender, EventArgs e)
23+
{
24+
if (dataGrid?.Handler?.PlatformView is FrameworkElement nativeElement)
25+
{
26+
nativeElement.KeyUp += DataGrid_KeyUpEvent;
27+
nativeElement.KeyDown += DataGrid_KeyDownEvent;
28+
}
29+
}
30+
31+
private void DataGrid_KeyUpEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
32+
{
33+
if (e.Key == Windows.System.VirtualKey.Shift)
34+
{
35+
customRowSelectionController._isShiftPressed = false;
36+
}
37+
}
38+
39+
private void DataGrid_KeyDownEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
40+
{
41+
if (e.Key == Windows.System.VirtualKey.Shift)
42+
{
43+
customRowSelectionController._isShiftPressed = true;
44+
}
45+
}
46+
#endif
47+
48+
}
49+
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core.Hosting;
3+
4+
namespace SfDatagridSelection;
5+
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}

0 commit comments

Comments
 (0)