How to navigate two rows at a time using custom selection controller in .NET MAUI DataGrid?
In this article, we will show you how to navigate two rows at a time using a custom selection controller in the .NET MAUI DataGrid.
xaml
<ContentPage.BindingContext>
<local:EmployeeViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>
<syncfusion:SfDataGrid x:Name="dataGrid"
Margin="10"
SelectionMode="Single"
NavigationMode="Row"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
ColumnWidthMode="Auto"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Employees}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
Format="#"
HeaderText="Employee ID" />
<syncfusion:DataGridTextColumn MappingName="Name"
HeaderText="Employee Name" />
<syncfusion:DataGridTextColumn MappingName="Title"
HeaderText="Designation" />
<syncfusion:DataGridDateColumn MappingName="HireDate"
HeaderText="Hire Date" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
C#
The code below illustrates a controller that provides customized navigation, allowing the Down arrow key to skip two rows on each press, while leaving all other key navigation unchanged in the DataGrid.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
dataGrid.SelectionController = new CustomRowSelectionController(this.dataGrid);
}
}
public class CustomRowSelectionController : DataGridRowSelectionController
{
public CustomRowSelectionController(SfDataGrid dataGrid) : base(dataGrid)
{
}
protected override void ProcessKeyDown(KeyEventArgs args, bool isCtrlKeyPressed, bool isShiftKeyPressed)
{
if (args.Key == KeyboardKey.Down)
{
var downArgs = new KeyEventArgs(KeyboardKey.Down)
{
Handled = false
};
base.ProcessKeyDown(downArgs, isCtrlKeyPressed, isShiftKeyPressed);
base.ProcessKeyDown(downArgs, isCtrlKeyPressed, isShiftKeyPressed);
}
else
{
base.ProcessKeyDown(args, isCtrlKeyPressed, isShiftKeyPressed);
}
}
}
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to navigate two rows at a time using a custom selection controller in .NET MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid’s feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!