How to Freeze the First and Last Columns in .NET MAUI DataGrid?
In this article, we will show you how to freeze the first and last columns in .NET MAUI DataGrid.
xaml
The code below demonstrates how to freeze the first and last columns in a DataGrid.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<syncfusion:SfDataGrid x:Name="sfGrid"
Grid.Column="0"
VerticalScrollBarVisibility="Never"
FrozenColumnCount="1"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
ColumnWidthMode="Auto"
AutoGeneratingColumn="sfGrid1_AutoGeneratingColumn"
ItemsSource="{Binding Employees}">
</syncfusion:SfDataGrid>
<syncfusion:SfDataGrid x:Name="sfGrid1"
Grid.Column="1"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
ColumnWidthMode="Auto"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Employees}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="EmployeeID"
Format="#"
HeaderText="Employee ID" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</Grid>
Xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
sfGrid.GetVisualContainer().ScrollOwner.Scrolled += ScrollOwner_Scrolled;
sfGrid1.GetVisualContainer().ScrollOwner.Scrolled += ScrollOwner_Scrolled1;
}
private async void ScrollOwner_Scrolled(object? sender, ScrolledEventArgs e)
{
var scrollView = sfGrid1.GetVisualContainer().ScrollOwner;
if (scrollView != null)
{
await scrollView.ScrollToAsync(scrollView.ScrollX, e.ScrollY, false);
}
}
private async void ScrollOwner_Scrolled1(object? sender, ScrolledEventArgs e)
{
var scrollView = sfGrid.GetVisualContainer().ScrollOwner;
if (scrollView != null)
{
await scrollView.ScrollToAsync(scrollView.ScrollX, e.ScrollY, false);
}
}
private void sfGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column.MappingName == "EmployeeID")
{
e.Cancel = true;
}
}
}
Output
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to freeze the first and last columns in .NET MAUI DataGrid.
You can refer to our .NET MAUI DataGrid 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, you can 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.
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, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!