|
1 | | -# How-to-add-a-row-with-auto-scrolling-behavior-in-.NET-MAUI-DataGrid-SfDataGrid |
2 | | -This demo shows how to add a row with auto scrolling behavior in .NET MAUI DataGrid (SfDataGrid)? |
| 1 | +# How to add a row with auto scrolling behavior in .NET MAUI DataGrid SfDataGrid |
| 2 | +This demo shows how to add a row with auto scrolling behavior in the Syncfusion [.NET MAUI DataGrid](https://help.syncfusion.com/maui/datagrid/overview) `SfDataGrid`. |
| 3 | + |
| 4 | +This sample helps you to to auto scroll the DataGrid to the last item added in the collection. In this sample, after adding the new data to the grid, the scrolling was performed automatically. |
| 5 | + |
| 6 | +## MainPage.xaml |
| 7 | +``` |
| 8 | + <ContentPage.BindingContext> |
| 9 | + <local:OrderInfoRepository x:Name="viewModel" /> |
| 10 | +</ContentPage.BindingContext> |
| 11 | +
|
| 12 | +<Grid RowDefinitions="50,*"> |
| 13 | + <Button Text="AddRow" WidthRequest="200" |
| 14 | + Clicked="Button_Clicked" /> |
| 15 | + |
| 16 | + <syncfusion:SfDataGrid x:Name="dataGrid" Grid.Row="1" |
| 17 | + ColumnWidthMode="Fill" |
| 18 | + ItemsSource="{Binding OrderInfoCollection}"> |
| 19 | + |
| 20 | + </syncfusion:SfDataGrid> |
| 21 | +</Grid> |
| 22 | +``` |
| 23 | + |
| 24 | +## MainPage.xaml.cs |
| 25 | +``` |
| 26 | +public partial class MainPage : ContentPage |
| 27 | +{ |
| 28 | + public MainPage() |
| 29 | + { |
| 30 | + InitializeComponent(); |
| 31 | + dataGrid.Loaded += DataGrid_Loaded; |
| 32 | + } |
| 33 | +
|
| 34 | + private void DataGrid_Loaded(object? sender, EventArgs e) |
| 35 | + { |
| 36 | + dataGrid.View!.CollectionChanged += MainPage_CollectionChanged; |
| 37 | + } |
| 38 | +
|
| 39 | + private async void MainPage_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
| 40 | + { |
| 41 | + if (e.Action == NotifyCollectionChangedAction.Add) |
| 42 | + { |
| 43 | + await Task.Delay(500); |
| 44 | + await dataGrid.ScrollToRowIndex(viewModel.OrderInfoCollection.Count - 1); |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + private void Button_Clicked(object sender, EventArgs e) |
| 49 | + { |
| 50 | + OrderInfo order = new OrderInfo("2001", "Andrew Fuller", "France", "BLONP", "Strasbourg"); |
| 51 | + viewModel.OrderInfoCollection.Add(order); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +Executing the code example above yields the following output |
| 58 | + |
| 59 | +<img src="https://github.com/user-attachments/assets/14be5823-e5c8-44af-8c9d-0ba973de9856" /> |
| 60 | + |
| 61 | + |
| 62 | +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). |
| 63 | + |
| 64 | +### Conclusion |
| 65 | +I hope you enjoyed learning about how to add a row with auto scroll in SfDataGrid. |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +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! |
0 commit comments