Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 113 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
# How-to-perform-range-selection-with-shift-key-in-.NET-MAUI-DataGrid-SfDataGrid
This demo shows how to perform range selection with shift key in .NET MAUI DataGrid (SfDataGrid)?
# How to perform range selection with shift key in .NET MAUI DataGrid SfDataGrid
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.

## Xaml
```
<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel" />
</ContentPage.BindingContext>

<ContentPage.Content>
<syncfusion:SfDataGrid x:Name="dataGrid"
SelectionMode="Multiple"
ItemsSource="{Binding OrderInfoCollection}">
</syncfusion:SfDataGrid>
</ContentPage.Content>
```

## Xaml.cs
```
public partial class MainPage : ContentPage
{
readonly CustomRowSelectionController customRowSelectionController;

public MainPage()
{
InitializeComponent();
customRowSelectionController = new CustomRowSelectionController(dataGrid);
dataGrid.SelectionController = customRowSelectionController;
#if WINDOWS
dataGrid.HandlerChanged += DataGrid_HandlerChanged;
#endif
}
#if WINDOWS
private void DataGrid_HandlerChanged(object? sender, EventArgs e)
{
if (dataGrid?.Handler?.PlatformView is FrameworkElement nativeElement)
{
nativeElement.KeyUp += DataGrid_KeyUpEvent;
nativeElement.KeyDown += DataGrid_KeyDownEvent;
}
}

private void DataGrid_KeyUpEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Shift)
{
customRowSelectionController._isShiftPressed = false;
}
}

private void DataGrid_KeyDownEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Shift)
{
customRowSelectionController._isShiftPressed = true;
}
}
#endif
}
```

## CustomRowSelectionController.cs
```
public class CustomRowSelectionController : DataGridRowSelectionController
{
private SfDataGrid grid;
public bool _isShiftPressed { get; set; }

public CustomRowSelectionController(SfDataGrid dataGrid) : base(dataGrid)
{
this.grid = dataGrid;
}

public override void HandlePointerOperation(RowColumnIndex rowColumnIndex)
{
if (_isShiftPressed)
{
if (grid.SelectedRows.Count > 0)
{
var selectedRow = grid.SelectedRows[0] as OrderInfo;
var selectCollection = new ObservableCollection<object>();
var startIndex = (grid.ItemsSource as ObservableCollection<OrderInfo>)!.IndexOf(selectedRow);
var endIndex = grid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
for (int i = startIndex; i <= endIndex; i++)
{
selectCollection.Add(grid.View!.Records[i].Data);
}
grid.SelectedRows = selectCollection;
return;
}

}
base.HandlePointerOperation(rowColumnIndex);
}
}
```

### ScreenShot

Here is the expected output when executing the sample:

<img src="https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjQwOTc0Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.je6YEmZlMfStikxEWG73znnJzUvvE8_G1T-RQfMlTUg" width = 404 height = 396/>

[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-perform-range-selection-with-shift-key-in-.NET-MAUI-DataGrid-SfDataGrid)

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).

### Conclusion
I hope you enjoyed learning about How to implement select all checkbox column in SfDataGrid.

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.

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!
25 changes: 25 additions & 0 deletions SfDatagridSelection/SfDatagridSelection.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35806.99 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfDatagridSelection", "SfDatagridSelection\SfDatagridSelection.csproj", "{EFCE8E56-B139-4744-B03F-62059AE3A3FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFCE8E56-B139-4744-B03F-62059AE3A3FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {45C581CD-38F1-4C2D-9A6E-E714D9D71D53}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SfDatagridSelection"
x:Class="SfDatagridSelection.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
15 changes: 15 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SfDatagridSelection
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}

protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new AppShell());
}
}
}
15 changes: 15 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="SfDatagridSelection.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SfDatagridSelection"
Shell.FlyoutBehavior="Flyout"
Title="SfDatagridSelection">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SfDatagridSelection
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Syncfusion.Maui.Data;
using Syncfusion.Maui.DataGrid;
using Syncfusion.Maui.DataGrid.Helper;
using Syncfusion.Maui.GridCommon.ScrollAxis;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SfDataGridSelection
{

public class CustomRowSelectionController : DataGridRowSelectionController
{
private SfDataGrid grid;
public bool _isShiftPressed { get; set; }

public CustomRowSelectionController(SfDataGrid dataGrid) : base(dataGrid)
{
this.grid = dataGrid;
}

public override void HandlePointerOperation(RowColumnIndex rowColumnIndex)
{
if (_isShiftPressed)
{
if (grid.SelectedRows.Count > 0)
{
var selectedRow = grid.SelectedRows[0] as OrderInfo;
var selectCollection = new ObservableCollection<object>();
var startIndex = (grid.ItemsSource as ObservableCollection<OrderInfo>)!.IndexOf(selectedRow);
var endIndex = grid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
for (int i = startIndex; i <= endIndex; i++)
{
selectCollection.Add(grid.View!.Records[i].Data);
}
grid.SelectedRows = selectCollection;
return;
}

}
base.HandlePointerOperation(rowColumnIndex);
}
}
}
19 changes: 19 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SfDatagridSelection.MainPage"
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
xmlns:local="clr-namespace:SfDataGridSelection">

<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel" />
</ContentPage.BindingContext>

<ContentPage.Content>
<syncfusion:SfDataGrid x:Name="dataGrid"
SelectionMode="Multiple"
ItemsSource="{Binding OrderInfoCollection}">
</syncfusion:SfDataGrid>
</ContentPage.Content>

</ContentPage>
50 changes: 50 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#if WINDOWS
using Microsoft.UI.Xaml;
#endif
using SfDataGridSelection;

namespace SfDatagridSelection
{
public partial class MainPage : ContentPage
{
readonly CustomRowSelectionController customRowSelectionController;

public MainPage()
{
InitializeComponent();
customRowSelectionController = new CustomRowSelectionController(dataGrid);
dataGrid.SelectionController = customRowSelectionController;
#if WINDOWS
dataGrid.HandlerChanged += DataGrid_HandlerChanged;
#endif
}
#if WINDOWS
private void DataGrid_HandlerChanged(object? sender, EventArgs e)
{
if (dataGrid?.Handler?.PlatformView is FrameworkElement nativeElement)
{
nativeElement.KeyUp += DataGrid_KeyUpEvent;
nativeElement.KeyDown += DataGrid_KeyDownEvent;
}
}

private void DataGrid_KeyUpEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Shift)
{
customRowSelectionController._isShiftPressed = false;
}
}

private void DataGrid_KeyDownEvent(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Shift)
{
customRowSelectionController._isShiftPressed = true;
}
}
#endif

}

}
26 changes: 26 additions & 0 deletions SfDatagridSelection/SfDatagridSelection/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.Logging;
using Syncfusion.Maui.Core.Hosting;

namespace SfDatagridSelection;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

#if DEBUG
builder.Logging.AddDebug();
#endif

return builder.Build();
}
}
Loading