How to add row with auto scrolling behavior in .NET MAUI SfDataGrid?
This article explains how to add a new row to the Syncfusion .NET MAUI DataGrid with automatic scrolling behavior.
To automatically scroll the DataGrid
when a new row is added to the collection at runtime, use the ScrollToRowIndex method. This method scrolls the view to the last row in the DataGrid, ensuring the newly added row is visible to the user.
xaml
<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel" />
</ContentPage.BindingContext>
<Grid RowDefinitions="50,*">
<Button Text="AddRow" WidthRequest="200"
Clicked="Button_Clicked" />
<syncfusion:SfDataGrid x:Name="dataGrid" Grid.Row="1"
ColumnWidthMode="Fill"
ItemsSource="{Binding OrderInfoCollection}">
</syncfusion:SfDataGrid>
</Grid>
xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
dataGrid.Loaded += DataGrid_Loaded;
}
private void DataGrid_Loaded(object? sender, EventArgs e)
{
dataGrid.View!.CollectionChanged += MainPage_CollectionChanged;
}
private async void MainPage_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
await Task.Delay(500);
await dataGrid.ScrollToRowIndex(viewModel.OrderInfoCollection.Count - 1);
}
}
private void Button_Clicked(object sender, EventArgs e)
{
OrderInfo order = new OrderInfo("2001", "Andrew Fuller", "UK", "BLONP", "London");
viewModel.OrderInfoCollection.Add(order);
}
}
Executing the above code example yields the following output
View Sample in GitHub.
Take a moment to explore this documentation, where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
Conclusion
I hope you enjoyed learning about how to add a row with auto scroll in SfDataGrid.
You can refer to our .NET MAUI DataGrid’s feature tour page to learn about its other groundbreaking feature representations. You can also 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, you can 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!